Look At C# 6 Using Static Feature

This feature will allow us to use a static member of a type without having to qualify it with the class name. It also helps to import a specific extension method instead of all the extension methods within a namespace. Let’s understand it with an example.

Create a new VS 2015 C# Console Application and name it as CSharp6UsingStaticDemo. Add the code, given below-



Here, we are accessing the static member method WriteLine() and Enum’s properties without qualifying the class name by adding “using static” statement for Sytem.Console and System.DayOfweek.

In case of conflict, where the same method name exists in both imported types, we need to qualify it with the full name.



Let’s understand how to use this feature on extension methods by adding the two methods, given below, in Extensionmethod.cs-



Let’s use the extension method declared in ExtensionMethodOneClass without importing ExtensionMethodTwoClass-



Prior to C# 6, we used to import the namespace, which will import all the extension methods of it.



This feature imports only accessible static members and nested types declared in the specified type. Inherited members are not imported and makes the extension methods declared in the specified type available for the extension method lookup.

For more details on this issue, please refer here.

I am ending the things here, I hope this is informative.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now