SlideShare a Scribd company logo
1.NET Meetup 2017 Syntactic sugar of C#
SEP 30, 2017
Syntactic sugar of C#:
language
improvements in
latest versions
by Dmitry Vereskun
2.NET Meetup 2017 Syntactic sugar of C#
• Properties enhancements
• Lambda expressions
• Initializers
• Number literals
• Inline variables
• Null expressions
• Throw expression
• Local functions
• Ref locals and ref returns
• Value tuples
• Pattern matching
• Async improvements
• Default keyword
AGENDA
C# 6.0 (VS 2015)
C# 7.0 (VS 2017)
C# 7.1 (VS 2017.3)
6
7
7.1
3.NET Meetup 2017 Syntactic sugar of C#
PROPERTIES
private string _value;
public string GetValue()
{
return _value;
}
public void SetValue(string value)
{
_value = value;
}
private string _value;
public string Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
4.NET Meetup 2017 Syntactic sugar of C#
AUTO-PROPERTIES
private string _value;
public string Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
public string Value
{
get;
set;
}
public string Value { get; set; }
or
5.NET Meetup 2017 Syntactic sugar of C#
GETTER-ONLY PROPERTIES AND METHODS
private string _value;
public string DoubleValue
{
get
{
return _value * 2;
}
}
private string _value;
public string DoubleValue => _value * 2;
public int GetResult()
{
return evaluate_result();
}
public int GetResult()
=> evaluate_result();
6
6.NET Meetup 2017 Syntactic sugar of C#
LAMBDAS EVERYWHERE
private string _value;
public string Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
private string _value;
public string Value
{
get => _value;
set => _value = value;
}
public MyClass(int n)
{
_n = n;
}
public MyClass(int n) => _n = n;
7
7.NET Meetup 2017 Syntactic sugar of C#
AUTO-PROPERTY INITIALIZERS AND READ-ONLY PROPERTIES
private string _value = "N/A";
public string Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
public string Value { get; set; } = "N/A";
6
private readonly int _onlyFive = 5;
public int OnlyFive
{
get
{
return _onlyFive;
}
}
public int OnlyFive { get; } = 5;
8.NET Meetup 2017 Syntactic sugar of C#
SINGLETON IN 2 (OR 3) LINES OF CODE
public class Singleton
{
private Singleton() { }
public static Singleton Instance { get; } = new Singleton();
}
public class Singleton
{
private Singleton() { }
static Singleton() => Instance = new Singleton();
public static Singleton Instance { get; }
}
6
9.NET Meetup 2017 Syntactic sugar of C#
• Classic
• New
DICTIONARY INITIALIZERS
var capitals = new Dictionary<string, string>
{
{ "Russia", "Moscow" }, // call Add
{ "Belarus", "Minsk" },
{ "Ukraine", "Kyiv" },
{ "USA", "Washington, D.C." },
};
var capitals = new Dictionary<string, string>
{
["Russia"] = "Moscow", // use indexer
["Belarus"] = "Minsk",
["Ukraine"] = "Kyiv",
["USA"] = "Washington, D.C.",
};
6
10.NET Meetup 2017 Syntactic sugar of C#
• Type literals:
– float (2.3f)
– double (4d)
– decimal (12.6m)
– long (25L)
– uint (1000u)
– ulong (13ul)
• Integer literals:
– Decimal (123)
– Hexadecimal (0x7b)
• What’s new?
– Binary integer literal (0b01111011)
– Digit group separator: _
Works with any number literals (100_000)
NUMBER LITERALS
int maxValue = 2_147_483_647;
byte mask = 0b0111_10011;
7
11.NET Meetup 2017 Syntactic sugar of C#
• Before:
• After:
or
INLINE VARIABLES
int intValue;
if (int.TryParse(str, out intValue))
{
// some actions with intValue
}
if (int.TryParse(str, out int intValue))
{
// some actions with intValue
}
if (int.TryParse(str, out var intValue))
{
// some actions with intValue
}
7
12.NET Meetup 2017 Syntactic sugar of C#
• Null coalescing operator can be applied for any nullable types
NULL EXPRESSIONS » NULL COALESCING OPERATOR ??
string s = GetValue();
if (s == null) s = "Default value";
string s = GetValue() ?? "Default value";
int? parentId = reader["parentId"] as int?;
int pId = parentId.HasValue ? parentId.Value : -1;
int pId = (reader["parentId"] as int?) ?? -1;
13.NET Meetup 2017 Syntactic sugar of C#
• So called Safe navigation operator or Elvis operator
• If left operand is not null, the operator acts as a regular dot operator
• If left operand is null, it returns a default value for expected type
NULL EXPRESSIONS » NULL-CONDITIONAL OPERATOR ?.
Type of foo.bar Type of foo?.bar
void void
Nullable type (incl. reference types) Same nullable type
Non-nullable type (structs, enums) Nullable<T> wrapper
6
14.NET Meetup 2017 Syntactic sugar of C#
SAFE DELEGATE INVOCATION WITH NULL-CONDITIONAL OPERATOR
protected virtual void OnComplete()
{
EventHandler complete = Complete;
if (complete != null)
{
complete(this, EventArgs.Empty);
}
}
protected virtual void OnComplete()
=> Complete?.Invoke(this, EventArgs.Empty);
15.NET Meetup 2017 Syntactic sugar of C#
NULL EXPRESSIONS » NULL-CONDITIONAL OPERATOR ?.
if (user != null)
{
user.Show();
}
int? age = (user != null)
? user.Age
: default(int?);
string name = (user != null && user.Name != null)
? user.Name
: "Guest";
user?.Show();
int? age = user?.Age;
string name = user?.Name ?? "Guest";
6
16.NET Meetup 2017 Syntactic sugar of C#
NULL EXPRESSIONS » NULL-CONDITIONAL OPERATOR ?[]
User[] users = GetUsers();
if (users != null)
{
Show(users[0].Name);
}
else
{
Show("no users");
}
User[] users = GetUsers();
Show(users?[0].Name ?? "no users");
User[] users = GetUsers();
if (users != null && users.Length > 0)
{
Show(users[0].Name);
}
else
{
Show("no users");
}
User[] users = GetUsers();
Show(users?.FirstOrDefault()?.Name);
6
17.NET Meetup 2017 Syntactic sugar of C#
• Throw can be placed in any place instead of ordinary expression
• Best to union it with ?: or ?? operators
THROW EXPRESSION
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
this.name = name;
this.name = name ?? throw new ArgumentNullException(nameof(name));
string first = args?.Length > 0
? args[0]
: throw new ArgumentException("Array is null or empty", nameof(args));
7
18.NET Meetup 2017 Syntactic sugar of C#
LOCAL FUNCTIONS
public static void QuickSort(int[] arr)
{
if (arr == null) throw new ArgumentNullException(nameof(arr));
if (arr.Length == 0) return;
SortSegment(0, arr.Length - 1);
void SortSegment(int from, int to)
{ … }
void SwapAndMove(ref int i, ref int j)
{ … }
}
void SwapAndMove(ref int i, ref int j)
{
int temp = arr[i];
arr[i++] = arr[j];
arr[j--] = temp;
}
void SortSegment(int from, int to)
{
int mid = arr[from + (to - from) / 2],
i = from, j = to;
while (i <= j)
{
while (arr[i] < mid) i++;
while (arr[j] > mid) j--;
if (i <= j) SwapAndMove(ref i, ref j);
}
if (i < to) SortSegment(i, to);
if (from < j) SortSegment(from, j);
}
7
19.NET Meetup 2017 Syntactic sugar of C#
public static void QuickSort(int[] arr)
{
if (arr == null) throw new ArgumentNullException(nameof(arr));
if (arr.Length == 0) return;
SortSegment(0, arr.Length - 1);
void SortSegment(int from, int to)
{
int mid = arr[from + (to - from) / 2], i = from, j = to;
while (i <= j)
{
while (arr[i] < mid) i++;
while (arr[j] > mid) j--;
if (i <= j) SwapAndMove(ref i, ref j);
}
if (i < to) SortSegment(i, to);
if (from < j) SortSegment(from, j);
}
void SwapAndMove(ref int i, ref int j)
{
int temp = arr[i];
arr[i++] = arr[j];
arr[j--] = temp;
}
}
Complete QuickSort with local functions
20.NET Meetup 2017 Syntactic sugar of C#
REF LOCALS AND REF RETURNS
private static ref int Max(ref int x, ref int y)
{
if (x > y)
{
return ref x;
}
else
{
return ref y;
}
}
int a = 123;
int b = 456;
Max(ref a, ref b) += 100;
Console.WriteLine(b); // 556!
7
21.NET Meetup 2017 Syntactic sugar of C#
REF LOCALS AND REF RETURNS
private static ref int Max(int[] array)
{
int max = 0;
for (int i = 1; i < array.Length; i++)
{
if (array[i] > array[max]) max = i;
}
return ref array[max];
}
int[] arr = { 3, 1, 4, 1, 5, 9, 2, 6, 5 };
Max(arr) = 0;
7
22.NET Meetup 2017 Syntactic sugar of C#
VALUE TUPLES
private static (int min, int max) MinAndMax(int[] array)
{
int min = array[0], max = min;
for (int i = 1; i < array.Length; i++)
{
if (array[i] < min) min = array[i];
if (array[i] > max) max = array[i];
}
return (min, max);
}
int[] arr = { 1, 4, 3, 6, 5, 8, 9 };
var minMax = MinAndMax(arr); // minMax.min and minMax.max
(int min, int max) minMax = MinAndMax(arr); // same as previous
(int, int) minMax = MinAndMax(arr); // minMax.Item1 and minMax.Item2
(int min, int max) = MinAndMax(arr); // min and max — deconstruction
(int min, _) = MinAndMax(arr); // min only
7
23.NET Meetup 2017 Syntactic sugar of C#
TUPLE ELEMENT NAMES INFERENCE
int count = 5;
string label = "Colors used in the map";
var pair = (count: count, label: label);
// old good C# 7.0. You have to define properties’ names
7.1
int count = 5;
string label = "Colors used in the map";
var pair = (count, label);
// element names are "count" and "label“. Inferred in C# 7.1
24.NET Meetup 2017 Syntactic sugar of C#
VALUE TUPLE DECONSTRUCTION
public class Point
{
public int X { get; set; }
public int Y { get; set; }
public void Deconstruct(out int x, out int y)
{
x = X;
y = Y;
}
}
Point p = GetPoint();
(int x, int y) = p; // deconstruction
7
25.NET Meetup 2017 Syntactic sugar of C#
ORDINARY CLASS, NOTHING STRANGE… OH, WAIT~
public class Point
{
public int X { get; set; }
public int Y { get; set; }
public static bool operator ==(Point p1, Point p2)
=> p1.X == p2.X && p1.Y == p2.Y;
public static bool operator !=(Point p1, Point p2)
=> !(p1 == p2);
public override bool Equals(object obj)
{
Point p = obj as Point;
return (p != null && this == p);
}
public override int GetHashCode() => X ^ Y;
}
26.NET Meetup 2017 Syntactic sugar of C#
ORDINARY CLASS, NOTHING STRANGE… OH, WAIT~
public class Point
{
public int X { get; set; }
public int Y { get; set; }
public static bool operator ==(Point p1, Point p2)
=> p1.X == p2.X && p1.Y == p2.Y;
public static bool operator !=(Point p1, Point p2)
=> !(p1 == p2);
public override bool Equals(object obj)
{
Point p = obj as Point;
return (p != null && this == p);
}
public override int GetHashCode() => X ^ Y;
}
27.NET Meetup 2017 Syntactic sugar of C#
PATTERN MATCHING » NULL TEMPLATE
public class Point
{
public int X { get; set; }
public int Y { get; set; }
public static bool operator ==(Point p1, Point p2)
=> p1.X == p2.X && p1.Y == p2.Y;
public static bool operator !=(Point p1, Point p2)
=> !(p1 == p2);
public override bool Equals(object obj)
{
Point p = obj as Point;
return !(p is null) && (this == p);
}
public override int GetHashCode() => X ^ Y;
}
7
28.NET Meetup 2017 Syntactic sugar of C#
PATTERN MATCHING » TYPE TEMPLATE
public class Point
{
public int X { get; set; }
public int Y { get; set; }
public static bool operator ==(Point p1, Point p2)
=> p1.X == p2.X && p1.Y == p2.Y;
public static bool operator !=(Point p1, Point p2)
=> !(p1 == p2);
public override bool Equals(object obj)
{
return (obj is Point p) && (this == p);
}
public override int GetHashCode() => X ^ Y;
}
7
29.NET Meetup 2017 Syntactic sugar of C#
PATTERN MATCHING
if (shape is null)
{
// show NULL error
}
else if (shape is Rectangle r)
{
// work with Rectangle r
}
else if (shape is Circle c)
{
// work with Circle c
}
7
30.NET Meetup 2017 Syntactic sugar of C#
PATTERN MATCHING
public static void SwitchPattern(object obj)
{
switch (obj)
{
case null:
Console.WriteLine("Constant pattern"); break;
case Person p when p.FirstName == "Dmitry":
Console.WriteLine("Person Dmitry"); break;
case Person p:
Console.WriteLine($"Other person {p.FirstName}, not Dmitry"); break;
case var x when x.GetType().IsGeneric:
Console.WriteLine($"Var pattern with generic type {x.GetType().Name}"); break;
case var x:
Console.WriteLine($"Var pattern with the type {x.GetType().Name}"); break;
}
}
7
31.NET Meetup 2017 Syntactic sugar of C#
GENERALIZED ASYNC RETURN
public async ValueTask<int> TakeFiveSlowly()
{
await Task.Delay(100);
return 5;
}
7
Now async methods can return ANY type with “async pattern”— GetAwaiter()
32.NET Meetup 2017 Syntactic sugar of C#
ASYNC MAIN
static int Main()
{
return DoAsyncWork().GetAwaiter().GetResult();
}
static async Task<int> Main()
{
return await DoAsyncWork();
}
static async Task Main()
{
await DoAsyncWork();
}
7.1
33.NET Meetup 2017 Syntactic sugar of C#
DEFAULT KEYWORD
Func<string, bool> whereClause = default(Func<string, bool>);
void SomeMethod(int? arg = default)
{
// ...
}
7.1
Func<string, bool> whereClause = default;
34.NET Meetup 2017 Syntactic sugar of C#
• C# 7.2
– Read-only references and structs
– Blittable types
– Ref-like types (stack only)
– Non-trailing named arguments
– Private protected access modifier
• C# 8
– Nullable reference types
– Default interface methods (who says Java?)
– Async streams
– Extension everything
WHAT ARE WE WAITING FOR?
https://channel9.msdn.com/Blogs/Seth-Juarez/A-Preview-of-C-8-with-Mads-Torgersen
35.NET Meetup 2017 Syntactic sugar of C#
Thanks for attention!
Questions? Suggestions?
Author: Dmitry Vereskun
ROKO Labs, Saratov, Russia
Telegram: d_vereskun

More Related Content

PDF
High performance web programming with C++14
PPTX
Effective C#
DOC
Pads lab manual final
PDF
Fun with Lambdas: C++14 Style (part 1)
PDF
C programs
PDF
C++ Programming - 11th Study
PDF
C program
PDF
C++ Programming - 1st Study
High performance web programming with C++14
Effective C#
Pads lab manual final
Fun with Lambdas: C++14 Style (part 1)
C programs
C++ Programming - 11th Study
C program
C++ Programming - 1st Study

What's hot (20)

DOCX
C++ file
PPTX
Lambda Expressions in C++
PDF
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
PDF
Stl algorithm-Basic types
PPT
Cquestions
PDF
Что нам готовит грядущий C#7?
PDF
.NET 2015: Будущее рядом
PDF
Inheritance and polymorphism
PDF
Static and const members
PPTX
PVS-Studio team experience: checking various open source projects, or mistake...
PDF
Data Structures Practical File
PDF
Automatically Describing Program Structure and Behavior (PhD Defense)
PDF
PDF
Implementing stack
PDF
Writing good std::future&lt;c++>
PDF
The present and the future of functional programming in c++
PDF
Understanding storage class using nm
PPSX
What's New In C# 7
PPSX
C# 6.0 - April 2014 preview
PPTX
Category theory, Monads, and Duality in the world of (BIG) Data
C++ file
Lambda Expressions in C++
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
Stl algorithm-Basic types
Cquestions
Что нам готовит грядущий C#7?
.NET 2015: Будущее рядом
Inheritance and polymorphism
Static and const members
PVS-Studio team experience: checking various open source projects, or mistake...
Data Structures Practical File
Automatically Describing Program Structure and Behavior (PhD Defense)
Implementing stack
Writing good std::future&lt;c++>
The present and the future of functional programming in c++
Understanding storage class using nm
What's New In C# 7
C# 6.0 - April 2014 preview
Category theory, Monads, and Duality in the world of (BIG) Data
Ad

Similar to Дмитрий Верескун «Синтаксический сахар C#» (20)

PPTX
C# 6 and 7 and Futures 20180607
PPTX
Roslyn and C# 6.0 New Features
PDF
Effective Object Oriented Design in Cpp
PDF
C++ manual Report Full
PPSX
What's new in C# 6 - NetPonto Porto 20160116
PPT
C++: Constructor, Copy Constructor and Assignment operator
PPTX
Novidades do c# 7 e 8
PPT
TechTalk - Dotnet
PDF
The Present and The Future of Functional Programming in C++
PPTX
Roslyn: el futuro de C#
PPTX
Roslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
PDF
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
PPTX
New C# features
PPTX
.NET Foundation, Future of .NET and C#
PDF
GECon2017_Cpp a monster that no one likes but that will outlast them all _Ya...
PDF
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
PDF
C# What's next? (7.x and 8.0)
PPTX
What’s new in .NET
PDF
Design Patterns - Compiler Case Study - Hands-on Examples
PDF
C# 7.x What's new and what's coming with C# 8
C# 6 and 7 and Futures 20180607
Roslyn and C# 6.0 New Features
Effective Object Oriented Design in Cpp
C++ manual Report Full
What's new in C# 6 - NetPonto Porto 20160116
C++: Constructor, Copy Constructor and Assignment operator
Novidades do c# 7 e 8
TechTalk - Dotnet
The Present and The Future of Functional Programming in C++
Roslyn: el futuro de C#
Roslyn: el futuro de C# y VB.NET by Rodolfo Finochietti
TDC2018SP | Trilha .Net - Novidades do C# 7 e 8
New C# features
.NET Foundation, Future of .NET and C#
GECon2017_Cpp a monster that no one likes but that will outlast them all _Ya...
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
C# What's next? (7.x and 8.0)
What’s new in .NET
Design Patterns - Compiler Case Study - Hands-on Examples
C# 7.x What's new and what's coming with C# 8
Ad

More from SpbDotNet Community (20)

PPTX
Станислав Флусов «Sharing общих сборок между процессами и доменами в IIS»
PPTX
Егор Гришечко «У вас найдётся минутка, чтобы поговорить о блокчейне?»
PDF
Константин Васильев «Fody против рутины»
PDF
Павел Федотовский «Как мы разрабатывали приложение для DotNetRu на Xamarin.Fo...
PDF
Слава Бобик «NancyFx для самых маленьких»
PDF
Иван Кожин «Saritasa Tools или ещё один подход к архитектуре приложения»
PDF
Игорь Лабутин «Коллекционируем данные в .NET»
PDF
Станислав Сидристый «Шаблон Lifetime: для сложного Disposing»
PDF
Артём Акуляков - F# for Data Analysis
PPTX
Анатолий Кулаков «The Metrix has you…»
PPTX
Роман Неволин «Провайдеры типов без боли и магии»
PDF
Александр Саитов «Основы профилирования и оптимизации приложений в .NET»
PDF
Сергей Лёвкин «Технологии Microsoft для актуальных трендов»
PDF
Алексей Мерсон «Domain Driven Design: профит малой кровью»
PPTX
Егор Гришечко «Async/Await и всё, что вы боялись спросить»
PPTX
Михаил Щербаков «Что может быть проще: делегаты и события»
PDF
Никита Каменский «Есть ли жизнь с UWP?»
PPTX
Александр Кугушев «Roslyn: очевидные неочевидности»
PPTX
ДотаНетоЛогия: СПб 2017
PDF
Юрий Волков «VM via F#»
Станислав Флусов «Sharing общих сборок между процессами и доменами в IIS»
Егор Гришечко «У вас найдётся минутка, чтобы поговорить о блокчейне?»
Константин Васильев «Fody против рутины»
Павел Федотовский «Как мы разрабатывали приложение для DotNetRu на Xamarin.Fo...
Слава Бобик «NancyFx для самых маленьких»
Иван Кожин «Saritasa Tools или ещё один подход к архитектуре приложения»
Игорь Лабутин «Коллекционируем данные в .NET»
Станислав Сидристый «Шаблон Lifetime: для сложного Disposing»
Артём Акуляков - F# for Data Analysis
Анатолий Кулаков «The Metrix has you…»
Роман Неволин «Провайдеры типов без боли и магии»
Александр Саитов «Основы профилирования и оптимизации приложений в .NET»
Сергей Лёвкин «Технологии Microsoft для актуальных трендов»
Алексей Мерсон «Domain Driven Design: профит малой кровью»
Егор Гришечко «Async/Await и всё, что вы боялись спросить»
Михаил Щербаков «Что может быть проще: делегаты и события»
Никита Каменский «Есть ли жизнь с UWP?»
Александр Кугушев «Roslyn: очевидные неочевидности»
ДотаНетоЛогия: СПб 2017
Юрий Волков «VM via F#»

Recently uploaded (20)

PDF
Salesforce Agentforce AI Implementation.pdf
PDF
Types of Token_ From Utility to Security.pdf
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PPTX
Introduction to Windows Operating System
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PDF
Website Design Services for Small Businesses.pdf
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
PPTX
Tech Workshop Escape Room Tech Workshop
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PDF
AI Guide for Business Growth - Arna Softech
PDF
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
PDF
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
PDF
Visual explanation of Dijkstra's Algorithm using Python
PDF
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
PDF
How Tridens DevSecOps Ensures Compliance, Security, and Agility
PPTX
Cybersecurity: Protecting the Digital World
PDF
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
Salesforce Agentforce AI Implementation.pdf
Types of Token_ From Utility to Security.pdf
Monitoring Stack: Grafana, Loki & Promtail
Introduction to Windows Operating System
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
Website Design Services for Small Businesses.pdf
Oracle Fusion HCM Cloud Demo for Beginners
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
Designing Intelligence for the Shop Floor.pdf
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
Tech Workshop Escape Room Tech Workshop
Advanced SystemCare Ultimate Crack + Portable (2025)
AI Guide for Business Growth - Arna Softech
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
Visual explanation of Dijkstra's Algorithm using Python
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
How Tridens DevSecOps Ensures Compliance, Security, and Agility
Cybersecurity: Protecting the Digital World
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025

Дмитрий Верескун «Синтаксический сахар C#»

  • 1. 1.NET Meetup 2017 Syntactic sugar of C# SEP 30, 2017 Syntactic sugar of C#: language improvements in latest versions by Dmitry Vereskun
  • 2. 2.NET Meetup 2017 Syntactic sugar of C# • Properties enhancements • Lambda expressions • Initializers • Number literals • Inline variables • Null expressions • Throw expression • Local functions • Ref locals and ref returns • Value tuples • Pattern matching • Async improvements • Default keyword AGENDA C# 6.0 (VS 2015) C# 7.0 (VS 2017) C# 7.1 (VS 2017.3) 6 7 7.1
  • 3. 3.NET Meetup 2017 Syntactic sugar of C# PROPERTIES private string _value; public string GetValue() { return _value; } public void SetValue(string value) { _value = value; } private string _value; public string Value { get { return _value; } set { _value = value; } }
  • 4. 4.NET Meetup 2017 Syntactic sugar of C# AUTO-PROPERTIES private string _value; public string Value { get { return _value; } set { _value = value; } } public string Value { get; set; } public string Value { get; set; } or
  • 5. 5.NET Meetup 2017 Syntactic sugar of C# GETTER-ONLY PROPERTIES AND METHODS private string _value; public string DoubleValue { get { return _value * 2; } } private string _value; public string DoubleValue => _value * 2; public int GetResult() { return evaluate_result(); } public int GetResult() => evaluate_result(); 6
  • 6. 6.NET Meetup 2017 Syntactic sugar of C# LAMBDAS EVERYWHERE private string _value; public string Value { get { return _value; } set { _value = value; } } private string _value; public string Value { get => _value; set => _value = value; } public MyClass(int n) { _n = n; } public MyClass(int n) => _n = n; 7
  • 7. 7.NET Meetup 2017 Syntactic sugar of C# AUTO-PROPERTY INITIALIZERS AND READ-ONLY PROPERTIES private string _value = "N/A"; public string Value { get { return _value; } set { _value = value; } } public string Value { get; set; } = "N/A"; 6 private readonly int _onlyFive = 5; public int OnlyFive { get { return _onlyFive; } } public int OnlyFive { get; } = 5;
  • 8. 8.NET Meetup 2017 Syntactic sugar of C# SINGLETON IN 2 (OR 3) LINES OF CODE public class Singleton { private Singleton() { } public static Singleton Instance { get; } = new Singleton(); } public class Singleton { private Singleton() { } static Singleton() => Instance = new Singleton(); public static Singleton Instance { get; } } 6
  • 9. 9.NET Meetup 2017 Syntactic sugar of C# • Classic • New DICTIONARY INITIALIZERS var capitals = new Dictionary<string, string> { { "Russia", "Moscow" }, // call Add { "Belarus", "Minsk" }, { "Ukraine", "Kyiv" }, { "USA", "Washington, D.C." }, }; var capitals = new Dictionary<string, string> { ["Russia"] = "Moscow", // use indexer ["Belarus"] = "Minsk", ["Ukraine"] = "Kyiv", ["USA"] = "Washington, D.C.", }; 6
  • 10. 10.NET Meetup 2017 Syntactic sugar of C# • Type literals: – float (2.3f) – double (4d) – decimal (12.6m) – long (25L) – uint (1000u) – ulong (13ul) • Integer literals: – Decimal (123) – Hexadecimal (0x7b) • What’s new? – Binary integer literal (0b01111011) – Digit group separator: _ Works with any number literals (100_000) NUMBER LITERALS int maxValue = 2_147_483_647; byte mask = 0b0111_10011; 7
  • 11. 11.NET Meetup 2017 Syntactic sugar of C# • Before: • After: or INLINE VARIABLES int intValue; if (int.TryParse(str, out intValue)) { // some actions with intValue } if (int.TryParse(str, out int intValue)) { // some actions with intValue } if (int.TryParse(str, out var intValue)) { // some actions with intValue } 7
  • 12. 12.NET Meetup 2017 Syntactic sugar of C# • Null coalescing operator can be applied for any nullable types NULL EXPRESSIONS » NULL COALESCING OPERATOR ?? string s = GetValue(); if (s == null) s = "Default value"; string s = GetValue() ?? "Default value"; int? parentId = reader["parentId"] as int?; int pId = parentId.HasValue ? parentId.Value : -1; int pId = (reader["parentId"] as int?) ?? -1;
  • 13. 13.NET Meetup 2017 Syntactic sugar of C# • So called Safe navigation operator or Elvis operator • If left operand is not null, the operator acts as a regular dot operator • If left operand is null, it returns a default value for expected type NULL EXPRESSIONS » NULL-CONDITIONAL OPERATOR ?. Type of foo.bar Type of foo?.bar void void Nullable type (incl. reference types) Same nullable type Non-nullable type (structs, enums) Nullable<T> wrapper 6
  • 14. 14.NET Meetup 2017 Syntactic sugar of C# SAFE DELEGATE INVOCATION WITH NULL-CONDITIONAL OPERATOR protected virtual void OnComplete() { EventHandler complete = Complete; if (complete != null) { complete(this, EventArgs.Empty); } } protected virtual void OnComplete() => Complete?.Invoke(this, EventArgs.Empty);
  • 15. 15.NET Meetup 2017 Syntactic sugar of C# NULL EXPRESSIONS » NULL-CONDITIONAL OPERATOR ?. if (user != null) { user.Show(); } int? age = (user != null) ? user.Age : default(int?); string name = (user != null && user.Name != null) ? user.Name : "Guest"; user?.Show(); int? age = user?.Age; string name = user?.Name ?? "Guest"; 6
  • 16. 16.NET Meetup 2017 Syntactic sugar of C# NULL EXPRESSIONS » NULL-CONDITIONAL OPERATOR ?[] User[] users = GetUsers(); if (users != null) { Show(users[0].Name); } else { Show("no users"); } User[] users = GetUsers(); Show(users?[0].Name ?? "no users"); User[] users = GetUsers(); if (users != null && users.Length > 0) { Show(users[0].Name); } else { Show("no users"); } User[] users = GetUsers(); Show(users?.FirstOrDefault()?.Name); 6
  • 17. 17.NET Meetup 2017 Syntactic sugar of C# • Throw can be placed in any place instead of ordinary expression • Best to union it with ?: or ?? operators THROW EXPRESSION if (name == null) { throw new ArgumentNullException(nameof(name)); } this.name = name; this.name = name ?? throw new ArgumentNullException(nameof(name)); string first = args?.Length > 0 ? args[0] : throw new ArgumentException("Array is null or empty", nameof(args)); 7
  • 18. 18.NET Meetup 2017 Syntactic sugar of C# LOCAL FUNCTIONS public static void QuickSort(int[] arr) { if (arr == null) throw new ArgumentNullException(nameof(arr)); if (arr.Length == 0) return; SortSegment(0, arr.Length - 1); void SortSegment(int from, int to) { … } void SwapAndMove(ref int i, ref int j) { … } } void SwapAndMove(ref int i, ref int j) { int temp = arr[i]; arr[i++] = arr[j]; arr[j--] = temp; } void SortSegment(int from, int to) { int mid = arr[from + (to - from) / 2], i = from, j = to; while (i <= j) { while (arr[i] < mid) i++; while (arr[j] > mid) j--; if (i <= j) SwapAndMove(ref i, ref j); } if (i < to) SortSegment(i, to); if (from < j) SortSegment(from, j); } 7
  • 19. 19.NET Meetup 2017 Syntactic sugar of C# public static void QuickSort(int[] arr) { if (arr == null) throw new ArgumentNullException(nameof(arr)); if (arr.Length == 0) return; SortSegment(0, arr.Length - 1); void SortSegment(int from, int to) { int mid = arr[from + (to - from) / 2], i = from, j = to; while (i <= j) { while (arr[i] < mid) i++; while (arr[j] > mid) j--; if (i <= j) SwapAndMove(ref i, ref j); } if (i < to) SortSegment(i, to); if (from < j) SortSegment(from, j); } void SwapAndMove(ref int i, ref int j) { int temp = arr[i]; arr[i++] = arr[j]; arr[j--] = temp; } } Complete QuickSort with local functions
  • 20. 20.NET Meetup 2017 Syntactic sugar of C# REF LOCALS AND REF RETURNS private static ref int Max(ref int x, ref int y) { if (x > y) { return ref x; } else { return ref y; } } int a = 123; int b = 456; Max(ref a, ref b) += 100; Console.WriteLine(b); // 556! 7
  • 21. 21.NET Meetup 2017 Syntactic sugar of C# REF LOCALS AND REF RETURNS private static ref int Max(int[] array) { int max = 0; for (int i = 1; i < array.Length; i++) { if (array[i] > array[max]) max = i; } return ref array[max]; } int[] arr = { 3, 1, 4, 1, 5, 9, 2, 6, 5 }; Max(arr) = 0; 7
  • 22. 22.NET Meetup 2017 Syntactic sugar of C# VALUE TUPLES private static (int min, int max) MinAndMax(int[] array) { int min = array[0], max = min; for (int i = 1; i < array.Length; i++) { if (array[i] < min) min = array[i]; if (array[i] > max) max = array[i]; } return (min, max); } int[] arr = { 1, 4, 3, 6, 5, 8, 9 }; var minMax = MinAndMax(arr); // minMax.min and minMax.max (int min, int max) minMax = MinAndMax(arr); // same as previous (int, int) minMax = MinAndMax(arr); // minMax.Item1 and minMax.Item2 (int min, int max) = MinAndMax(arr); // min and max — deconstruction (int min, _) = MinAndMax(arr); // min only 7
  • 23. 23.NET Meetup 2017 Syntactic sugar of C# TUPLE ELEMENT NAMES INFERENCE int count = 5; string label = "Colors used in the map"; var pair = (count: count, label: label); // old good C# 7.0. You have to define properties’ names 7.1 int count = 5; string label = "Colors used in the map"; var pair = (count, label); // element names are "count" and "label“. Inferred in C# 7.1
  • 24. 24.NET Meetup 2017 Syntactic sugar of C# VALUE TUPLE DECONSTRUCTION public class Point { public int X { get; set; } public int Y { get; set; } public void Deconstruct(out int x, out int y) { x = X; y = Y; } } Point p = GetPoint(); (int x, int y) = p; // deconstruction 7
  • 25. 25.NET Meetup 2017 Syntactic sugar of C# ORDINARY CLASS, NOTHING STRANGE… OH, WAIT~ public class Point { public int X { get; set; } public int Y { get; set; } public static bool operator ==(Point p1, Point p2) => p1.X == p2.X && p1.Y == p2.Y; public static bool operator !=(Point p1, Point p2) => !(p1 == p2); public override bool Equals(object obj) { Point p = obj as Point; return (p != null && this == p); } public override int GetHashCode() => X ^ Y; }
  • 26. 26.NET Meetup 2017 Syntactic sugar of C# ORDINARY CLASS, NOTHING STRANGE… OH, WAIT~ public class Point { public int X { get; set; } public int Y { get; set; } public static bool operator ==(Point p1, Point p2) => p1.X == p2.X && p1.Y == p2.Y; public static bool operator !=(Point p1, Point p2) => !(p1 == p2); public override bool Equals(object obj) { Point p = obj as Point; return (p != null && this == p); } public override int GetHashCode() => X ^ Y; }
  • 27. 27.NET Meetup 2017 Syntactic sugar of C# PATTERN MATCHING » NULL TEMPLATE public class Point { public int X { get; set; } public int Y { get; set; } public static bool operator ==(Point p1, Point p2) => p1.X == p2.X && p1.Y == p2.Y; public static bool operator !=(Point p1, Point p2) => !(p1 == p2); public override bool Equals(object obj) { Point p = obj as Point; return !(p is null) && (this == p); } public override int GetHashCode() => X ^ Y; } 7
  • 28. 28.NET Meetup 2017 Syntactic sugar of C# PATTERN MATCHING » TYPE TEMPLATE public class Point { public int X { get; set; } public int Y { get; set; } public static bool operator ==(Point p1, Point p2) => p1.X == p2.X && p1.Y == p2.Y; public static bool operator !=(Point p1, Point p2) => !(p1 == p2); public override bool Equals(object obj) { return (obj is Point p) && (this == p); } public override int GetHashCode() => X ^ Y; } 7
  • 29. 29.NET Meetup 2017 Syntactic sugar of C# PATTERN MATCHING if (shape is null) { // show NULL error } else if (shape is Rectangle r) { // work with Rectangle r } else if (shape is Circle c) { // work with Circle c } 7
  • 30. 30.NET Meetup 2017 Syntactic sugar of C# PATTERN MATCHING public static void SwitchPattern(object obj) { switch (obj) { case null: Console.WriteLine("Constant pattern"); break; case Person p when p.FirstName == "Dmitry": Console.WriteLine("Person Dmitry"); break; case Person p: Console.WriteLine($"Other person {p.FirstName}, not Dmitry"); break; case var x when x.GetType().IsGeneric: Console.WriteLine($"Var pattern with generic type {x.GetType().Name}"); break; case var x: Console.WriteLine($"Var pattern with the type {x.GetType().Name}"); break; } } 7
  • 31. 31.NET Meetup 2017 Syntactic sugar of C# GENERALIZED ASYNC RETURN public async ValueTask<int> TakeFiveSlowly() { await Task.Delay(100); return 5; } 7 Now async methods can return ANY type with “async pattern”— GetAwaiter()
  • 32. 32.NET Meetup 2017 Syntactic sugar of C# ASYNC MAIN static int Main() { return DoAsyncWork().GetAwaiter().GetResult(); } static async Task<int> Main() { return await DoAsyncWork(); } static async Task Main() { await DoAsyncWork(); } 7.1
  • 33. 33.NET Meetup 2017 Syntactic sugar of C# DEFAULT KEYWORD Func<string, bool> whereClause = default(Func<string, bool>); void SomeMethod(int? arg = default) { // ... } 7.1 Func<string, bool> whereClause = default;
  • 34. 34.NET Meetup 2017 Syntactic sugar of C# • C# 7.2 – Read-only references and structs – Blittable types – Ref-like types (stack only) – Non-trailing named arguments – Private protected access modifier • C# 8 – Nullable reference types – Default interface methods (who says Java?) – Async streams – Extension everything WHAT ARE WE WAITING FOR? https://channel9.msdn.com/Blogs/Seth-Juarez/A-Preview-of-C-8-with-Mads-Torgersen
  • 35. 35.NET Meetup 2017 Syntactic sugar of C# Thanks for attention! Questions? Suggestions? Author: Dmitry Vereskun ROKO Labs, Saratov, Russia Telegram: d_vereskun