how will c# 7 probably look like

23
Kakšen bo verjetno C# 7? Damir Arh, Razum d.o.o., Microsoft MVP

Upload: damir-arh

Post on 22-Jan-2018

218 views

Category:

Software


0 download

TRANSCRIPT

Page 1: How Will C# 7 Probably Look Like

Kakšen bo verjetno C# 7?

Damir Arh, Razum d.o.o., Microsoft MVP

Page 2: How Will C# 7 Probably Look Like

Agenda

• C# danes

• Novosti v C# 7

• Visual Studio „15“

Page 3: How Will C# 7 Probably Look Like

Kratka zgodovina C#

Generiki(2005)

LINQ(2007)

dynamic(2010)

async(2012)

Roslyn(2015)

Page 4: How Will C# 7 Probably Look Like

Kaj je Roslyn?

• Nova implementacija prevajalnika

• Prevajalnik kot storitev

• Odprtokodni projekt

Page 5: How Will C# 7 Probably Look Like

Ključne teme C# 7

• Delo s podatki

• Učinkovitost

• Poenostavitev

Page 6: How Will C# 7 Probably Look Like
Page 7: How Will C# 7 Probably Look Like

Ujemanje vzorcev

• Objektni jeziki

– Vnaprej znane operacije

– Enostavno dodajanje podatkovnih tipov

– Polimorfizem

• Funkcionalni jeziki

– Vnaprej znani podatkovni tipi

– Enostavno dodajanje operacij

– Ujemanje vzorcev

Page 8: How Will C# 7 Probably Look Like

Visual Studio „15“ Preview 4

Ujemanje vzorcev

Page 9: How Will C# 7 Probably Look Like

Kaj smo videli?

• Operator „is“ z deklaracijo spremenljivke

• Stavek „switch“ z ločevanjem po tipu

• Dodatni pogoj v stavku „case“

Page 10: How Will C# 7 Probably Look Like

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);

}

Page 11: How Will C# 7 Probably Look Like

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}");

Page 12: How Will C# 7 Probably Look Like

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}");

Page 13: How Will C# 7 Probably Look Like

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);

}

Page 14: How Will C# 7 Probably Look Like

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

Page 15: How Will C# 7 Probably Look Like

Optimizacija asinhronih klicev

private async ValueTask DoWorkAsync(bool doRealWork){

if (doRealWork){

await DoRealWorkAsync();}

}

Page 16: How Will C# 7 Probably Look Like

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;

}

Page 17: How Will C# 7 Probably Look Like

Izjeme v izrazih

public Grunt(int health) => _health = health >= 0 ? health :

throw new ArgumentOutOfRangeException();

Page 18: How Will C# 7 Probably Look Like

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;

Page 19: How Will C# 7 Probably Look Like

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

Page 20: How Will C# 7 Probably Look Like

Kaj je na voljo danes?

• Praktični preizkus

• Spremljanje objav na GitHubu

• Sodelovanje v diskusijah

• Prevod in preizkus izvorne kode

• …

Page 22: How Will C# 7 Probably Look Like

Viri

• http://bit.ly/vs15preview4

• https://github.com/dotnet/roslyn/

• http://bit.ly/RoslynLanguageRoadmap

• http://bit.ly/DncCSharp7

• http://bit.ly/DncRoslynOverview

Page 23: How Will C# 7 Probably Look Like

@DamirArh

http://www.damirscorner.com