Algorithm


In C#, we use the using keyword to import external resources (namespaces, classes, etc) inside a program. For example, see below-

Code Examples

#1 C# using Program Example

Code - C# Programming

// using System namespace
using System;

namespace Program {

  class Program1 {
    static void Main(string[] args) {
      Console.WriteLine("Hello World!");   
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Hello World!

#2 C# Programming using to create an alias

Code - C# Programming

// creating alias for System.Console
using Programiz = System.Console;

namespace HelloWorld {

  class Program {
    static void Main(string[] args) {

      // using Programiz alias instead of System.Console
      Programiz.WriteLine("Hello World!");    
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Hello World!

#3 Code Example- C# using static with System.Math in C# Programming

Code - C# Programming

using System;

// using static directive
using static System.Math;

namespace Program {  

  class Program1  {  
    public static void Main(string[] args)  {  
       	 
      double n  = Sqrt(9);
      Console.WriteLine("Square root of 9 is " + n);  
      	 
    }  
  }  
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Square root of 9 is 3

#4 Code Example- C# using static with System.Math in C# Programming

Code - C# Programming

using System; 
 
namespace Program {  

  class Program1 {  
    public static void Main(string[] args) {  

      // using the class name Math
      double n  = Math.Sqrt(9);
      Console.WriteLine("Square root of 9 is " + n);  
    }  
  }  
} 
Copy The Code & Try With Live Editor

Output

x
+
cmd
Square root of 9 is 3
Advertisements

Demonstration


C# using Program Example