Using Static Preferences and Conflicts: C# 6 And C# 7

This blog explains that if we are including “using static” as directive in C# 6 or C# 7 i.e., “Using types instead of namespaces as directive,” and you hav a local method with the same name as that of “using static” in that case how to resolve conflict and which method will get preferences.

As we know, before C# 6 we used to include only namespaces as “using directive.” But in C# 6 & C# 7 we can include static classes, enum, non-static classes & structs too as “using directive.” So in C# 6 & C# 7 we can include following types as using directive.

So we can say that C# 6 & C# 7 allows us “Using types instead of namespaces as directive.”

Now the question arises that if we are able to use any type as directive in that case if we have a local method with same name then which one will get preferences and how to resolve the conflicts. To understand it in a better way just take an example.

  1. using static System.Console;  
  2. using static System.Environment;  
  3.   
  4. namespace UsingStaticPreferences  
  5. {  
  6.     class Program  
  7.     {  
  8.         public static void WriteLine(string s)  
  9.         {  
  10.             Write(s);         //System.Console.Write  
  11.             Write(NewLine);  //System.Environment.NewLine  
  12.             ReadKey();  
  13.         }  
  14.         static void Main(string[] args)  
  15.         {  
  16.      Title = "Output of 'Using Static Preferences'"//System.Console.Title  
  17.             WriteLine($"Hi, Welcome to C# Corner"); //Program.WriteLine  
  18.         }  
  19.     }  

In the above example you can see that I have used “System.Console” as directive and I am using Write() method & Title property of System.Console.

But I am not using WriteLine() method of System.Console class. I am using our own WriteLine() method of Program class. I have not given class name before calling WriteLine() method even though it is referring to Program.WriteLine() method and not to System.Console.WriteLine() method. So by looking at the above code example we can say that we have got proof of 2 points.

1.   Always your own local method or property will get preference over method or property of type included in using directive.

But if you don’t have local method or property with that name then it will automatically refer to the methods & properties of types included as using directive. As you can see that WritiLine() method referring to local method whereas Write() method & “Title” property referring to System.Console.

2.   There is no conflict because if you are not giving fully qualified name while calling a method or property compiler decides automatically that which one has to be called.

Now you may be thinking what if you do not want to call your own local method or property but to call method or property of the types included in using directive. In that case you can call it very easily by just referring the complete class name as you can see in the below code snippet.

  1. using System;  
  2. using static System.Console;  
  3. using static System.Environment;  
  4.   
  5. namespace UsingStaticPreferences  
  6. {  
  7.     class Program  
  8.     {  
  9.         public static void WriteLine(string s)  
  10.         {  
  11.             Write(s);           //System.Console.Write  
  12.             Write(NewLine);     //System.Environment.NewLine  
  13.             ReadKey();          //Console.ReadKey  
  14.         }  
  15.         static void Main(string[] args)  
  16.         {  
  17.             Title = "Output of 'Using Static Preferences'"//System.Console.Title  
  18.             Console.WriteLine($"This method is referring to System.Console.WriteLine"); //Console.WriteLine  
  19.         }  
  20.     }  

Next Recommended Reading
Look At C# 6 Using Static Feature