Sunday, 14 May 2017

using static in C# 6.0

Often we use a class’s static methods throughout our code. Repeatedly typing the class name can obscure the meaning of your code. A common example is when you write classes that perform many numeric calculations. Your code will be littered with @System.Math.Sin, @System.Math.Sqrt and other calls to different methods in the Math class.
In C# 6.0 , we can go one step further and import the static members of a class into our namespace.This means we can use the static members of a class directly with no need to qualify them with their namespace or type name.
1
2
    using static System.Math;
using static System.Console;
it can eliminate a lot of redundant or superfluous type identifiers in the code and thus help improve readability
1
2
3
4
5
public void UsingStaticFeature()
{
    var power = Pow(2, 2);
    WriteLine(power);
}

No comments:

Post a Comment