C# Using Static

王林
王林Original
2024-09-03 15:30:36926browse

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.

 Working of using static directive in C#

  • Using static directive was introduced in 2015 with the release of C# version 6.
  • The static members can be referenced without needing to reference the namespace or the class by using static directive.
  • The nested types can also be referenced by making use of static directive.

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:

C# Using Static

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.

Examples of C# Using Static

Here are the following examples mention below

Example #1

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:

C# Using Static

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.

Example #2

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:

C# Using Static

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.

Advantages of C# Using Static

There are several advantages mentioned below:

  • If a member of a class is made static, that static member does not have to depend on any instance of the class. This avoids the creation of the new instance of the class when the member is to be called if not declared static. This again calls for garbage collection which can be avoided.
  • Implementation of utility methods like sending electronic mails, logging of errors, obtaining the value from web configuration, etc. can be made easier using static directive.
  • The usage of memory is less while using static directive because a static method is shared.

Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# StackNext article:C# Stack