Table of Contents
 Working of using static directive in C#
Examples of C# Using Static
Example #2
Advantages of C# Using Static
Conclusion

C# Using Static

Sep 03, 2024 pm 03:30 PM
c# c# tutorial

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>;
Copy after login

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");
}
}
Copy after login

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);
}
}
}
Copy after login

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");
}
}
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1675
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

C# vs. C  : History, Evolution, and Future Prospects C# vs. C : History, Evolution, and Future Prospects Apr 19, 2025 am 12:07 AM

The history and evolution of C# and C are unique, and the future prospects are also different. 1.C was invented by BjarneStroustrup in 1983 to introduce object-oriented programming into the C language. Its evolution process includes multiple standardizations, such as C 11 introducing auto keywords and lambda expressions, C 20 introducing concepts and coroutines, and will focus on performance and system-level programming in the future. 2.C# was released by Microsoft in 2000. Combining the advantages of C and Java, its evolution focuses on simplicity and productivity. For example, C#2.0 introduced generics and C#5.0 introduced asynchronous programming, which will focus on developers' productivity and cloud computing in the future.

See all articles