top of page

C# tips and tricks - part 3

  • 3634795
  • Oct 3, 2024
  • 1 min read

Updated: Oct 16, 2024

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.



ree




Amexis Team

Comments


Amexis Logo

Sofia, 6 Al. Zhendov str
Plovdiv, 26a Petko Karavelov str

info@amexis.net
+359 88 212 3791

Have you worked with us before?

Follow Us On:

  • LinkedIn
  • Facebook

© 

© 2024 by Amexis
All rights reserved.

© 2024 by Amexis
All rights reserved.

© 2024 by Amexis
All rights reserved.

bottom of page