top of page

C# tips and tricks - part 3

Updated: Oct 16

Static using declarations in C#

In C#, static using declarations are a convenient way to access static class members directly without having to specify a fully qualified name every time. This is especially useful when you are working with many static methods and properties of the same class, such as Math or Console.

By using a static using statement, you can import all static members of a class so that you can use them directly without specifying the class name. This functionality was introduced in C# 6.0.



using static System.Math;
using static System.Console;
class Program
{
    static void Main()
    {
        double result = Sqrt(25); // No need to write Math.Sqrt
        WriteLine(result); // same here
		Console.WriteLine
    }
}

Benefits of using static using declarations


  1. Shorter and readable code: reduces the need to write fully qualified class names.

  2. Easier to use commonly used static members: Convenient for using commonly used static methods such as those in the Math and Console classes.

  3. Improved readability: Especially useful for mathematical calculations where long class names can make formulas difficult to read.







Amexis Team

21 views0 comments

Comments


bottom of page