Upcoming features and enhancements in C# 7.2

Proposed features & enhancements in C# 7.2:

Non-trailing named arguments

Currently, a named argument can follow positional arguments

public bool DoPayment(ICard creditCard, decimal amount)
{
    //Some Code.
}

DoPayment(amount: 100, tax: 1, discount: 2); // This is already valid.
DoPayment(100, tax: 1, discount: 2); // This is already valid.
DoPayment(100, 1, discount: 2); // This is already valid.

But currently a positional argument cannot follow a named argument

DoPayment(amount: 100, 1, discount: 2); // This is not valid currently.
DoPayment(amount: 100, 1, 2); // This is not valid currently.
DoPayment(100, tax: 1, 2); // This is not valid currently.

As per proposed enhancements in C# 7.2, named arguments are allowed to be used in non-trailing position, as long as they are used in their correct position.

DoPayment(amount: 100, 1, discount: 2); // This would become valid in C# 7.2.
DoPayment(amount: 100, 1, 2); // This would become valid in C# 7.2.
DoPayment(100, tax: 1, 2); // This would become valid in C# 7.2.

private protected

Currently, C# supports following access modifiers:

private: can be accessed only in the same class or struct.

protected: can be accessed only in the same class or struct, or in a class that is derived from that class.

internal: can be accessed only in the same assembly.

protected internal: can be accessed in the assembly in which it is declared, or from within a derived class in another assembly.

With the proposed enhancement in C# 7.2, a member can be declared as private protected.  private protected member can be accessed within a subclass of its container if that subclass is in the same assembly as the member.

Readonly references (in parameters or ref readonly parameters)

Currently, Value-Type Parameters can be passed:

By Value – Passing a value-type variable to a method by value means passing a copy of the variable to the method. Any changes to the parameter that take place inside the method have no affect on the original data stored in the argument variable.

By Reference – If you want the called method to change the value of the parameter, you must pass it by reference, using the ref or out keyword.

With the proposed in parameters (ref readonly parameters) in C# 7.2, it would be possible to efficiently pass variables by reference, but without exposing the data to modifications.

public Point DoSomeCalculation(in Point point1, Point point2, ref Point point3)
{
	//Some Code.

	point1.X = 0; // not vaild (point1 is passed as in (ref readonly) parameter)

	point2.Y = point1.Y; // vaild (point2 is passed By Value)

	point3.X = point1.X; // vaild (point3 is passed By Reference)

	point1.X = point2.X; // not vaild (point1 is passed as in (ref readonly) parameter).

	Point c = new Point(point1.X, point2.Y + point3.Y); // vaild.

	//Some Code.

	return c;
}

conditional-ref

Pattern to conditionally bind a ref variable to one or another expression
The proposed syntax:

<condition> ? ref <consequence> : ref <alternative>;

 Compile time enforcement of safety for ref-like types



Will share full working sample codes in my future posts on each of  above enhancement once they are released. I am looking forward to see private protected and in parameters in action.



Happy Coding !!!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *