Wortstellung hold up two fingers…what letter does that look like? V = Verb.
How Will C# 7 Probably Look Like
Transcript of How Will C# 7 Probably Look Like
Kakšen bo verjetno C# 7?
Damir Arh, Razum d.o.o., Microsoft MVP
Agenda
• C# danes
• Novosti v C# 7
• Visual Studio „15“
Kratka zgodovina C#
Generiki(2005)
LINQ(2007)
dynamic(2010)
async(2012)
Roslyn(2015)
Kaj je Roslyn?
• Nova implementacija prevajalnika
• Prevajalnik kot storitev
• Odprtokodni projekt
Ključne teme C# 7
• Delo s podatki
• Učinkovitost
• Poenostavitev
Ujemanje vzorcev
• Objektni jeziki
– Vnaprej znane operacije
– Enostavno dodajanje podatkovnih tipov
– Polimorfizem
• Funkcionalni jeziki
– Vnaprej znani podatkovni tipi
– Enostavno dodajanje operacij
– Ujemanje vzorcev
Visual Studio „15“ Preview 4
Ujemanje vzorcev
Kaj smo videli?
• Operator „is“ z deklaracijo spremenljivke
• Stavek „switch“ z ločevanjem po tipu
• Dodatni pogoj v stavku „case“
Terke
public (int weight, int count) Stocktake(IEnumerable<IWeapon> weapons)
{var w = 0;var c = 0;foreach (var weapon in weapons){
w += weapon.Weight;c++;
}return (w, c);
}
Prirejanje vrednosti terk
var inventory = Stocktake(weapons);Debug.WriteLine($"Item count: {inventory.count}");Debug.WriteLine($"Item weight: {inventory.weight}");
(var inventoryWeight, var inventoryCount) =Stocktake(weapons);
Debug.WriteLine($"Item count: {inventoryCount}");Debug.WriteLine($"Item weight: {inventoryWeight}");
(var inventoryWeight, *) = Stocktake(weapons);Debug.WriteLine($"Item weight: {inventoryWeight}");
Razstavljanje tipov
public static void Deconstruct(this Sword sword, out int damage, out int durability)
{damage = sword.Damage;durability = sword.Durability;
}
var sword = new Sword {Damage = 5, Durability = 7};(var damage, var durability) = sword;Debug.WriteLine($"Sword damage rating: {damage}");Debug.WriteLine($"Sword durability: {durability}");
Lokalne funkcije
static void ReduceMagicalEffects(this IWeapon weapon, int timePassed)
{double decayRate = CalculateDecayRate();double GetRemainingEffect(double currentEffect) =>
currentEffect * Math.Pow(decayRate, timePassed);
weapon.FireEffect = GetRemainingEffect(weapon.FireEffect);
weapon.IceEffect = GetRemainingEffect(weapon.IceEffect);
}
public void LocalVariableByReference(){
var terrain = Terrain.Get();ref TerrainType terrainType =
ref terrain.GetAt(4, 2);Assert.AreEqual(TerrainType.Grass, terrainType);
terrain.BurnAt(4, 2);Assert.AreEqual(TerrainType.Dirt, terrainType);
}
public ref TerrainType GetAt(int x, int y) => ref terrain[x, y];
Vračanje vrednosti po referenci
Optimizacija asinhronih klicev
private async ValueTask DoWorkAsync(bool doRealWork){
if (doRealWork){
await DoRealWorkAsync();}
}
Telesa metod v obliki izrazov
public Grunt(int health) => _health = health >= 0 ? health : 0;
~Grunt() => Debug.WriteLine("Finalizer called");
private int _health;public int Health{
get => _health = 0;set => _health = value >= 0 ? value : 0;
}
Izjeme v izrazih
public Grunt(int health) => _health = health >= 0 ? health :
throw new ArgumentOutOfRangeException();
Izboljšave konstant
const int MAX_PLAYER_LEVEL = 0b0010_1010;const int MAX_PLAYER_XP = 10_000_000;const uint RED_COLOR = 0xFF_00_00_FF;
Visual Studio „15“ Preview 4
• Implementirana večina predstavljenih novosti
• Izjeme
– Ignoriranje vrnjenih vrednosti terk
– Optimizacija asinhronih klicev
– Telesa metod v obliki izrazov
– Izjeme v izrazih
• Podpora terkam zahteva dodaten paket
Kaj je na voljo danes?
• Praktični preizkus
• Spremljanje objav na GitHubu
• Sodelovanje v diskusijah
• Prevod in preizkus izvorne kode
• …
DNC Magazine
Viri
• http://bit.ly/vs15preview4
• https://github.com/dotnet/roslyn/
• http://bit.ly/RoslynLanguageRoadmap
• http://bit.ly/DncCSharp7
• http://bit.ly/DncRoslynOverview
@DamirArh
http://www.damirscorner.com