In C# 7, a lot of new features have been introduced, and also enhancement have been done for some features. Given below is the list of features which I am going to explain in this article. Learn – C# Basics
1. Tuples (Tuples Enhancement)
2. Out Parameter
4. Binary Literal
5. Throw Expressions
6. Digit Separators
7. Refer Local and Ref Functions
A tuple is an ordered, finite list of elements.To combine a set of values which do not have sufficient commonality to justify making a class.Commonly used ways,
• To represent a single set of data.
• To return multiple values from a method without using out parameters or ByRef parameters
• To pass multiple values to a method through a single parameter
• The tuple is a reference type.
• It is an immutable list of heterogeneous data values and all the properties read-only.
• There was no custom naming for internal data elements only default names as Item1, Item2 etc.
• Tuples are Value Type and mutable one.
• It provides semantic naming for each of these returned values, easier to use and more readable.
2. Out Parameter :
The out method parameter can be used to return multiple values from the method.Any changes made to the parameter in the caller method will be reflected in that variable when control passes back to the calling method.
To use an out parameter, both the method definition and the calling method must explicitly use the out keyword.The out keyword was used to pass a method argument’s reference.
Up to C# 6.0,
Before a variable is passed as an out argument, it must be declared.However, the called method is required to assign a value before the method returns.
But it is possible in C# 7.0,
Now we can declare variable directly when you are passing it inside a method.So that we can write less lines of code without adding complexity and it reduces the chances of using the variable improperly before it is returned.
Recommended Reading: Future of Dot Net Development
Many time we don’t want to access whole tuple bundle or we just need internal values then we can use Deconstruction features of C# 7.0, we can easily de-construct a tuple and fetch values.
4. Binary Literals
Until C# 6.0, we have Decimal literals, Hexadecimal literals, Character literals and String literals.There are no binary literals until C#6.0.The Convert class which can convert binary strings to various types of an integer.
C# 7.0 introduces binary literals, Using the 0b prefix a binary literal can be defined so that you can specify bit patterns directly.
Latest Update – Free PDF DOWNLOAD for Microsoft Dot Net Course Content
5.Throw Expression :
To throw an exception from the middle of an expression, first, have to check it is null or not.
Now, it is possible to throw an exception in the middle of an expression, which allows us to throw exceptions in places like in conditional expressions.
6. Digit Separators :
Just to help the readability of programs, the digit separator _ is available with C# 7.
The purpose of a digit separator is to improve readability, nothing more. The value of the variable is unaffected by this literal.
Adding this separator to a previously defined short, the bits can be grouped by e.g. every 4 digits:
var b = 0b1010_1011_1100_1101_1110_1111;
How you do the grouping doesn’t matter for the compiler, it’s just for readability. The following code snippet uses the separator every three digits.
var b = 0b1_101_100_101_111;
Digit Separators are not limited to binaries. You can use this separator with other numbers as well. Here, it is used with an int passing a hexadecimal value:
int X1 = 0x44aa_abcd;
Where the _ digit separator may not be used:
• at the beginning of the value (_121)
• at the end of the value (121_ or 121.05_)
• next to the decimal (10_.0)
• next to the exponent character (1.1e_1)
• next to the type specifier (10_f)
• immediately following the 0x or 0b in binary and hexadecimal literals (0b_1001_1000)
7. Ref Locals and Ref returns
Up to C#6.0
The ref keyword allows code to pass a value type variable by reference as a parameter of a method. Here the ref parameter, the calling method definition and the method definition must mainly use the ref keyword.
In C# 7.0, data can be stored and returned locally by the ref.
C# 7.0 implemented the reference ref keyword that can be used to return values by the ref. The ref is also used for storing values by the ref in the local variable.
Local variables can be declared with the ref modifier
Why is it useful?
Returned values by reference can improve performance in cases where the altered is copying resources. C# 7.0 helps to use the verifiably safe code while avoiding unnecessary copies.