C# using static usage

T.Zacks 3,996 Reputation points
2022-06-04T07:09:13.977+00:00

see the code first
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("javatpoint",".com");
Console.WriteLine(sqrt);
Console.WriteLine(newStr);
}
}
}

i understand that if i write using static System.String; then i do not have to write String before Concat function i guess it is good for static class. when we call static class function then we do not have to create instance rather we write static class name dot function name.

but using static may not be useful when i have to create instance of class before calling the function. am i right ?

am i right using static has utility only for static class ? please guide me. Thanks

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-06-04T08:01:12.77+00:00

    When using using static you now need to know properties and methods without preceding with the namespace/class. It's okay if you know the properties and methods e.g.

    Example 1, a method coded as static,

    using System.Text.RegularExpressions;
    
    namespace IncrementSequenceConsole.Classes
    {
        public class Helpers
        {
            public static string NextValue(string sender)
            {
                string value = Regex.Match(sender, "[0-9]+$").Value;
                return sender[..^value.Length] + (long.Parse(value) + 1)
                    .ToString().PadLeft(value.Length, '0');
            }
        }
    }
    

    Usage

    using System;
    using System.Runtime.CompilerServices;
    using IncrementSequenceConsole.Classes;
    
    namespace IncrementSequenceConsole
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine($"          F1124   {Helpers.NextValue("F1124")}");
                Console.WriteLine($"       1278-120   {Helpers.NextValue("1278-120")}");
                Console.WriteLine($"           F102   {Helpers.NextValue("F102")}");
                Console.WriteLine($"3999/IKL/VII/21   {Helpers.NextValue("3999/IKL/VII/21")}");
                Console.ReadLine();
            }
    
        }
    }
    

    Example 2 with static

    using System;
    using System.Runtime.CompilerServices;
    using static IncrementSequenceConsole.Classes.Helpers;
    
    namespace IncrementSequenceConsole
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine($"          F1124   {NextValue("F1124")}");
                Console.WriteLine($"       1278-120   {NextValue("1278-120")}");
                Console.WriteLine($"           F102   {NextValue("F102")}");
                Console.WriteLine($"3999/IKL/VII/21   {NextValue("3999/IKL/VII/21")}");
                Console.ReadLine();
            }
    
        }
    }
    

    If I add additional methods or properties I know them unlike say Math where I may not know all methods thus making it more difficult to find them.

    With that I'd go with an alias.

    using System;
    using System.Runtime.CompilerServices;
    using H = IncrementSequenceConsole.Classes.Helpers;
    
    namespace IncrementSequenceConsole
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine($"          F1124   {H.NextValue("F1124")}");
                Console.WriteLine($"       1278-120   {H.NextValue("1278-120")}");
                Console.WriteLine($"           F102   {H.NextValue("F102")}");
                Console.WriteLine($"3999/IKL/VII/21   {H.NextValue("3999/IKL/VII/21")}");
                Console.ReadLine();
            }
    
        }
    }
    

    My style of coding it's extremely rare to have a reason to instantiate an instance of a class of my own but when doing so side with aliasing.


1 additional answer

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2022-06-04T07:51:23.15+00:00

    I often use it to simplify code, like in the sample with PI and Pow from MSDN doc : static-modifier
    (in fact, it is VS which suggests the modification when I call some functions from other Namespaces...)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.