TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Using Static Directive

C# Using Static Directive for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C-SHARP

C# Using Static Directive (Static Import)

C# using static directive helps us to access static members (methods and fields) of the class without using the class name. If we don?t use static directive, we need to use class name to call static members each time.

It allows us to import static members of a class into the source file. It follows a syntax that is given below.

C# using static directive syntax

using static <fully-qualified-type-name>

In the following example, we are not using static directive. We can see that, to access static method of Math class, class name is used.

C# Example without using static directive

using System;
namespace CSharpFeatures
{
    class StaticImport
    {
        public static void Main(string[] args)
        {
            double sqrt   = Math.Sqrt(144); // Math class is used to access Sqrt() method
            string newStr = String.Concat("TheDeveloperBlog",".com");
            Console.WriteLine(sqrt);
            Console.WriteLine(newStr);
        }
    }
}

Output

12
TheDeveloperBlog.com

In the following example, we are using static directive in the source file. So, it does not require class name before calling the method.

C# Example with using static directive

using System;
using static System.Math; // static directive 
using static System.String;
namespace CSharpFeatures
{
    class StaticImport
    {
        public static void Main(string[] args)
        {
            double sqrt   = Sqrt(144); // Calling without class name
            string newStr = Concat("TheDeveloperBlog",".com");
            Console.WriteLine(sqrt);
            Console.WriteLine(newStr);
        }
    }
}

We can see, it produces the same result even after removing the type from the function call.

Output

12
TheDeveloperBlog.com





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf