C# Version 7.0 Announcements

In this article, you will learn about C# version 7.0 announcements.

There have been a number of changes, improvements and features made to C# 7.0 release of the next version of Visual Studio. In this article, I’m going to introduce the biggest features and changes, which have been announced. Thus, we all are quite familiar with the previous versions of C#, which are very useful language features to remove an unexceptional code and clean up our code.

Nowadays, programmers don’t want to suffer with error prone code or writing extra code --  they want an environment of convenient coding where they can get some simple and delightful code . We can see some features in C#,

  • Portability
  • Advanced runtime (garbage collection)
  • Easy to adopt
  • Native access
  • Fast execution
  • Reliability (type safety)
  • Powerful features (reflection and dependency injection)
  • Cutting edge (asynchronous programming)

C# 7.0 features come with the concise and shorter format for the wide use. C# 7.0 features are highly portable in a wide range of environments over the spectrum of mobile, Server computing, embedded and desktop. In this announcement, the features are highly expressive and it's easy to maintain the code. Here are a few new features in this announcement-

  • Multi-value Returns
  • Pattern Matching: Enhanced Switch Blocks
  • Pattern Matching: Decomposition
  • Tuple as a Mutable type
  • Partial Class Enhancements
  • Local Functions
  • Binary Literals
  • Ref Returns

There is No need to declare variables - In C# 7.0, there is no need to declare the variables, outside of the method from which, you are going to get it. It will be required only to add out before the variable and use it later on. Thus, it can be declared directly as the arguments such as a parameter.

code

Wildcards for ignoring Out parameters – This would be a great thing, as the idea of an out parameter, using wildcard* is of ignoring out parameters.

code

Pattern matching mechanism - This is a whole new mechanism, which will be introduced in C# 7, which for now will be used for checking  the objects in three types,

  • Constant patterns of the form c (where c is a constant expression in C#), which tests that the input is equal to c.
  • Type patterns of the form T x (where T is a type and x is an identifier), which tests that the input has type T and if so, it extracts the value of the input into a fresh variable x of type T.
  • Var patterns of the form var x (where x is an identifier), which always matches and simply puts the value of the input into a fresh variablex with the same type as the input.

code

Switch Case extended to use different object types - Switch case is no longer only using the primitive types, it is now also able to use custom types, as the example, given below shows-

code

Extended Tuples - Tuples are now extended to be more friendly. Now, you can also declare the tuple functions and can be returned in more friendly way. C# offered a tuple as a value type.

To create a tuple, you can use this syntax, given below-

  1. var sum = (5, 20);  
The main things about the tuples are-
  • You can specify the element names in tuple literals.
  • Their elements are public and mutable fields.
  • Two tuples can have the same hash code.

code

Deconstruction of tuples
- Deconstructing declaration is syntax for splitting a tuple (or other value) into its parts and assigning those parts individually to the fresh variables.

code

In this, you can use the variables in different manners such as-

  • Var inside parenthesis
  • Var outside of the parenthesis
  • Deconstructing assignment ((first, middle, last) = StudentName (studId1) ;)

You can define function inside method's body - Now, if the function is only used in one method, you can define in in the method's body.

code

Local functions are the functions, which we define inside another function. You can't create a delegate, which points to the local function.

Literal Improvements - C# 7.0 allows occurrence of a digit separator inside number literals, which means it is allowed for better readability along with the binary literals.
code

You can specify bit patterns as well binary literals.

code

Extended ref, you can return by ref - Until now, we were able to pass as ref one parameter. Now, we can also return them by the references and also store them as the reference. Thus, you can't return a reference to a local variable.

  1. var a = ref FourthElement(myArray)  
  2. a = 10; //MyArray[4] now equals 10  
code

More expression bodied members - There are new things here as well. Now, you can add a destructor to the expression bodies. Expression bodied members are available for both the methods and properties.

code

Throw exception inside expression - Now you can throw exception inside expression or middle of expression (in certain place).

code

These are the cool features of C# 7.0. Thanks for reading this article.