Home >Backend Development >C#.Net Tutorial >C# Using Static
The using static directive was introduced in 2016 with the release of C# version 6 which allows us to reference the members that are static without the necessity of namespace references or even the type references and using static directive can also be used to reference nested types. For example, by using static directives, the static members of the console class can be referenced by not referencing the class itself which results in a very simpler yet efficient code and using static directives also makes the code more readable and the static members of the class can be imported into a source file using static directive.
The syntax of using static directive in C# is as follows:
using static <fully-qualified-type-name>;
where fully-qualified-type-name is the type name whose members that are static and nested can be reference without needing to use the type itself.
Consider the example below to explain the usage of using static directive in C#:
using System.IO; //using static directive is defined for system.Console using static System.Console; //a class called Check is defined class Check { //Main method is called static void Main() { //WriteLine method is referenced without using the Console class name WriteLine("Welcome to using static directives"); } }
Output:
In the above program, using a static directive is defined for the system. Console. Then check is the class defined. Then the main method is called. Then the WriteLine method is referenced without using the Console class name because we have used static directive for the system. Console. The output of the program is as shown in the snapshot above.
Here are the following examples mention below
C# program to demonstrate the usage of using static directive in a program
Code:
//using static directive for system.console using static System.Console; //using static directive for system.math using static System.Math; //using static directive for system.string using static System.String; //a namespace called Features is defined namespace Features { //a class called import is defined class Import { //main method is called public static void Main(string[] args) { //the sqrt method is called without referencing math class because using static directive is used for system.math double sqroot = Sqrt(64); //the concat method is called without referencing math class because using static directive is used for system.string string newString = Concat("Learning"," is fun"); //the writeline method is called without referencing math class because using static directive is used for system.console WriteLine(sqroot); WriteLine(newString); } } }
Output:
In the above program, we have made use of static directive for system.console. Then we have made use of static directive for system.math. Then we have made use of static directive for system.string. Then a namespace called Features is defined. Then a class called import is defined. Then the main method is called. Then the sqrt method is called without referencing math class because using static directive is used for system.math. Then the concat method is called without referencing math class because using static directive is used for system.string. Then the writeline method is called without referencing math class because using static directive is used for system.console. The output of the program is as shown in the snapshot above.
C# program to demonstrate the usage of using static directive in a program
Code:
using System; //using static directive for system.console using static System.Console; //using static directive for system.string using static System.String; //a class called check is defined class check { //main method is called public static void Main(string[] args) { //the writeline method is called without referencing math class because using static directive is used for system.console WriteLine("Check if the given number is positive or negative or zero:"); //a variable number is defined int number = 10; //Comparison operator is used to check if the number is greater than zero if (number > 0) //the writeline method is called without referencing math class because using static directive is used for system.console WriteLine("Positive number"); //Comparison operator is used to check if the number is equal to zero else if (number == 0) //the writeline method is called without referencing math class because using static directive is used for system.console WriteLine("Zero"); else //the writeline method is called without referencing math class because using static directive is used for system.console WriteLine("Negative number"); } }
Output:
In the above program, we have made use of static directive for system.console. Then we have made use of static directive for system.string. Then a class called check is defined. Then the main method is called. Then the writeline method is called without referencing math class because using static directive is used for system.console. Then a variable number is defined to which the number is assigned which needs to be checked if it is positive, negative, or zero. Then the Comparison operator is used to check if the number is greater than zero or less than zero or equal to zero. The output of the program is as shown in the snapshot above.
There are several advantages mentioned below:
In this tutorial, we understand the concept of using static directive in C# through definition, the syntax of using static directive in C#, working of using static directive through programming examples and their outputs.
The above is the detailed content of C# Using Static. For more information, please follow other related articles on the PHP Chinese website!