Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
57
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

This method is shown in the following example:

bool error = false; Exception ex = null;

try

{

// Since C#5

await service.InitializeAsync();

}

catch (Exception e)

{

// Declare bool or place exception inside variable error = true;

ex = e;

}

// If you don't use the exception if (error)

{

// Handle async task

}

//If want to use information from the exception if (ex != null)

{

await logger.LogAsync(e);

}

//Close the service, since this isn't possible in the finally await service.CloseAsync();

Section 72.11: Minor changes and bugfixes

Parentheses are now forbidden around named parameters. The following compiles in C#5, but not C#6

Version ≤ 5.0

Console.WriteLine((value: 23));

Operands of is and as are no longer allowed to be method groups. The following compiles in C#5, but not C#6

Version ≤ 5.0

var result = "".Any is byte;

The native compiler allowed this (although it did show a warning), and in fact didn’t even check extension method compatibility, allowing crazy things like 1.Any is string or IDisposable.Dispose is object.

See this reference for updates on changes.

Section 72.12: Using an extension method for collection initialization

Collection initialization syntax can be used when instantiating any class which implements IEnumerable and has a method named Add which takes a single parameter.

GoalKicker.com – C# Notes for Professionals

437

In previous versions, this Add method had to be an instance method on the class being initialized. In C#6, it can

also be an extension method.

public class CollectionWithAdd : IEnumerable

{

public void Add<T>(T item)

{

Console.WriteLine("Item added with instance add method: " + item);

}

public IEnumerator GetEnumerator()

{

// Some implementation here

}

}

public class CollectionWithoutAdd : IEnumerable

{

public IEnumerator GetEnumerator()

{

// Some implementation here

}

}

public static class Extensions

{

public static void Add<T>(this CollectionWithoutAdd collection, T item)

{

Console.WriteLine("Item added with extension add method: " + item);

}

}

public class Program

{

public static void Main()

{

var collection1 = new CollectionWithAdd{1,2,3}; // Valid in all C# versions var collection2 = new CollectionWithoutAdd{4,5,6}; // Valid only since C# 6

}

}

This will output:

Item added with instance add method: 1

Item added with instance add method: 2

Item added with instance add method: 3

Item added with extension add method: 4

Item added with extension add method: 5

Item added with extension add method: 6

Section 72.13: Disable Warnings Enhancements

In C# 5.0 and earlier the developer could only suppress warnings by number. With the introduction of Roslyn Analyzers, C# needs a way to disable warnings issued from specific libraries. With C# 6.0 the pragma directive can suppress warnings by name.

Before:

GoalKicker.com – C# Notes for Professionals

438

#pragma warning disable 0501

C# 6.0:

#pragma warning disable CS0501

GoalKicker.com – C# Notes for Professionals

439