Date post: | 05-Jul-2015 |
Category: |
Software |
Upload: | antonio-pelleriti |
View: | 581 times |
Download: | 4 times |
Le novità di C# 6NUOVE FUNZIONALITA’ E CARATTERISTICHE DEL
LINGUAGGIO
Mi presento
Ing. Antonio Pelleriti
Twitter: @zetanove
Mail: [email protected]
Web: www.antoniopelleriti.it
Ultimo libro:
Programmare con C# 5, LSWR 2014
Un grazie a
Agenda
C# ieri e oggi
Le novità di C# 6
Demo
C# 1.0
2002
Prima versione di C#
C# 2.0
2005
Generics
Tipi parziali
Metodi anonimi
Iteratori
get/set accessibilità separata
Classi static
Altro ancora…
C# 3.0
2007
Variabili di tipo implicito
Inizializzatori di oggetti e di collezioni
Auto-properties
Tipi anonimi
Metodi di estensione
Query expressions
Espressioni Lambda
Metodi parziali
E altro ancora…
C# 4.0
2010
Dynamic binding
Argomenti denominati e opzionali
Generics co/controvarianza
E altro ancora…
C# 5.0
2012
Async
Caller info attributes
Altro…
Le novità di C# 6
Cosa vedremo in C# 6
Auto-Property Initializers
Using per membri statici
Exception filter
Index Inizializers (dizionari, ecc.)
Null propagation
Await in catch/finally
Expression-bodied members
Nameof
String interpolation
Forse in C# 6
E cosa forse vedremo in C# 6
Primary constructor
Indexed member access
Dichiarazione inline di variabili
Binary literals e separatori di cifre
Inizializzatori di eventi
params IEnumerable
Auto property initializers
Inizializzazione di proprietà automatiche
public class Persona
{
public string Nome{ get; set; }
public string Cognome{ get; set; }
}
= "Antonio";
= "Pelleriti"
Getter only auto property
initializers
Anche in sola lettura
public class Persona
{
public string Nome{ get; } = "Antonio";
}
public class Persona
{
public string Nome{ get; } //Errore in C# 5
public Persona(string nome)
{
Nome=nome; //Errore in C# 5
}
}
NB in VS 2014 CTP le proprietà get only possono essere inizializzatesolo con gli inizializzatori non nel costruttore
Using static members
Using di classe, permette di usarne i membri statici
using System.Console;
using System.Math;
class Program
{
static void Main()
{
WriteLine(Sqrt(3*3 + 4*4));
}
}
Exception Filter
Catturare un’eccezione se si verifica una condizione
catch(Exception ex) if(cond1)
{
}
catch(Exception ex1) if(cond2)
{
}
private static bool Log(Exception e)
{ /* log it */ ; return false; }
…
try { … } catch (Exception e) if (Log(e)) {}
Index initializer 1/1
Nuova sintassi per inizializzare dizionari o oggetti con indicizzatori
var numbers = new Dictionary<int, string>
{
{ 7 , "seven" },
{ 9 , "nine" },
{ 13 , "thirteen" },
};
var numbers2 = new Dictionary<int, string>
{
[7] = "seven",
[9] = "nine",
[13] = "thirteen"
};
Index initializer 1/2
Classe con indicizzatore
public class Person
{
public string this[int index]
{
get
{
return numbers[index];
}
set
{
numbers[index] = value;
}
}
}
Inizializzazione con indicizzatore
var persona = new Person("antonio")
{
[0] = "1628692",
[3] = "0299992",
};
Null conditional
Null Propagation operator ?
string str=…;
int? len;
if(str!=null)
len=str.Length;
int? len= str?.Length; //null se str==null
char? first=str?[0];
await in catch/finally
await in catch/finally
In genere errore di compilazione
Cannot await in the body of a catch clause
try{}
catch
{
await MetodoAsincrono(); //errore in C# 5
}
Expression bodies members
Utilizzo di espressioni lambda per implementare il corpo di
proprietà
public string Completo => String.Format("{0} {1}", Nome, Cognome);
metodo
public void Print(string title) => Console.WriteLine(title+" "+Nome + " " + Cognome);
operatore
public static implicit operator string (Person p) => p.Nome + " " + p.Cognome;
nameof
Operatore nameof
int x = 0;
string name=nameof(x);
Console.WriteLine("{0}={1}", name, x);
name=(nameof(Person.Address.ZipCode)); // returns "ZipCode"
DEMO
Visual Studio ’14’ CTP 4
prebuilt Azure VM images with VS "14" CTP 4 already installed.
Alcune features funzionanti in ctp solo con
<LangVersion>experimental</LangVersion>
In csproj
End User Preview on top of Visual Studio 2013.
(note: this VS 2013 preview is no longer updated)
Link utili
.NET Compiler Platform ("Roslyn")
http://roslyn.codeplex.com/
C# 6 Language Features status
https://roslyn.codeplex.com/wikipage?title=Language%20Feature%20Status
VS 2015 preview
http://www.visualstudio.com/en-us/downloads/visual-studio-2015-downloads-vs
promo
Programmare con C# 5
guida completa
http://amzn.to/1x7sGHc
“
”Domande