What's new in c#7.pptx
- Количество слайдов: 27
There should only need to be one code base in the world for understanding C# http: //bit. ly/dot. Net. Co. Evolution
Key new features Null. Conditional Operator async/await Caller information dynamic Named and optional arguments C# 3 LINQ Lambda Expression Trees Generics Anonymous methods Nullable types Managed C# 4 C# 5 C# 6 String interpolation C# 2 In progress C# 1 C# 7 Version nameof Expression body functions Auto-Property improvements Task Parallel Library Anonymous types Implicit typing (var) using static
C# 7 Generic programming FP Techniques Dynamic Typing Async programming Roslyn C# 6 OOP Generic programming FP Techniques Dynamic Typing Async programming Roslyn OOP Generic programming FP Techniques Dynamic Typing Async programming OOP Generic programming FP Techniques Dynamic Typing C# 3 OOP Generic programming FP Techniques OOP Generic programming OOP C# 4 C# 5 OOP C# 2 Techniques C# 1 Version Metaprogramming?
- finished - in-progress - finished https: //github. com/dotnet/roslyn/issues/2136
Allow _ as separator • var d = 123_456; • var x = 0 x. AB_CD_EF; Allow 0 b as binary number representation: • var b = 0 b 1010_1011_1100_1101_1110_1111;
static void Main(string[] args) { void For. Each<T>(IEnumerable<T> source, Action<T> action) { foreach (var item in source) { action? . Invoke(item); } } var list = new[] { "First", "Second", "Third", "Forth", "Fifth" }; For. Each(list, Console. Write. Line); Console. Read. Key(); }
int x; out x)) if(int. Try. Parse(Console. Read. Line(), out int x)) if(int. Try. Parse(Console. Read. Line(), { Console. Write. Line($"{nameof(x)} = {x}"); }
static void Print. Int(object o) { if (o is int i || (o is string s && int. Try. Parse(s, out i))) { Console. Write. Line(i); } }
static void Main(string[] args) { { var data = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var result = Get. Min. Max(data); Console. Write. Line($"Min: {result. Item 1} Max: {result. Item 2}"); Console. Write. Line($"Min: {result. Min} Max: {result. Max}"); Console. Read. Key(); } } static Tuple<T, T> Get. Min. Max<T>(IEnumerable<T> source) => static (T Min, T Max) Get. Min. Max<T>(IEnumerable<T> source) => Tuple. Create(source. Min(), source. Max()); PM> Install-Package System. Value. Tuple -Pre
static void Main(string[] args) { { var data = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var result = Get. Min. Max(data); var (min, max) = Get. Min. Max(data); Console. Write. Line($"Min: {result. Min} Max: {result. Max}"); Console. Read. Key(); } static (T Min, T Max) Get. Min. Max<T>(IEnumerable<T> source) => (source. Min(), source. Max()); Console. Write. Line($"Min: {min} Max: {max}"); Console. Read. Key(); } static (T Min, T Max) Get. Min. Max<T>(IEnumerable<T> source) => (source. Min(), source. Max());
public ref int Find(int number, int[] numbers) { for (int i = 0; i < numbers. Length; i++) { if (numbers[i] == number) { return ref numbers[i]; // return the storage location, not the value } } throw new Index. Out. Of. Range. Exception($"{nameof(number)} not found"); } void Main(string[] args) { int[] array = { 1, 15, -39, 0, 7, 14, -12 }; ref int place = ref Find(7, array); // aliases 7's place in the array place = 9; // replaces 7 with 9 in the array Console. Write. Line(array[4]); // prints 9 }
class Person { private static Concurrent. Dictionary<int, string> names = new Concurrent. Dictionary<int, string>(); private int id = Get. Id(); public Person(string name) =>names. Try. Add(id, name); }// constructors { names. Try. Add(id, name); ~Person() =>names. Try. Remove(id, out *); } { names. Try. Remove(id, out *); // destructors public string Name { get => names[id]; { return names[id]; } // getters set =>names[id] ==value; } { names[id] value; // setters } } }
static async Task Do. Work() { await. . . } static void Main() { Do. Work(). Get. Awaiter(). Get. Result(); } static async Task<int> Main(string[] args) { // User code goes here } static int $Generated. Main(string[] args) { return Main(args). Get. Awaiter(). Get. Result(); }
class Person(string First, string Last); class Person : IEquatable<Person> { public string First { get; } public string Last { get; } public Person(string First, string Last) { this. First = First; this. Last = Last; } public bool Equals(Person other) => other != null && First == other. First && Last == other. Last; public override bool Equals(object obj) => obj is Person other ? Equals(other) : false; public override int Get. Hash. Code() => Great. Hash. Function(First, Last); … }
var p 1 = new Point { X = 3, Y = 7 }; var p 2 = p 1 with { X = -p 1. X };
https: //github. com/dotnet/roslyn https: //blogs. msdn. microsoft. com/dotnet https: //blogs. msdn. microsoft. com/visualstudio/ https: //github. com/dotnet/coreclr http: //sergeyteplyakov. blogspot. com/
Code: https: //github. com/compilyator/CSharp 7 Slides: https: //goo. gl/4 YGz. He
What's new in c#7.pptx