Alias ​​for static member in C #?

I have a static member:

namespace MyLibrary { public static class MyClass { public static string MyMember; } } 

which I want to get as follows:

 using MyLibrary; namespace MyApp { class Program { static void Main(string[] args) { MyMember = "Some value."; } } } 

How to make MyMember accessible (without MyClass. ) Before MyApp simply adding using MyLibrary ?

+4
source share
1 answer

C # does not allow you to create member aliases, only from types. Thus, the only way to do something like this in C # would be to create a new property accessible from this area:

 class Program { static string MyMember { get { return MyClass.MyMember; } set { MyClass.MyMember = value; } } static void Main(string[] args) { MyMember = "Some value."; } } 

This is actually not an alias, but it executes the syntax you are looking for.

Of course, if you only access / change a member on MyClass and do not assign it, this can be simplified a bit:

 class Program { static List<string> MyList = MyClass.MyList; static void Main(string[] args) { MyList.Add("Some value."); } } 
+6
source

Source: https://habr.com/ru/post/1491999/


All Articles