Mikayil Abdullayev
Mikayil Abdullayev

Reputation: 12376

C# - A collection of constants

In Objective-C (probably in C too) I can have a header file which can contain just anything from macros to constants. And any class (actually the file it resides) that imports that header file can see and use whatever there is declared. Isn't there a similar way to do this in C#? I have not found anything helpful so far. What I can do in C# is have a collection of static classes each having constant fields in them. But when I want to access those constants I have no other way but say Classname.ConstantName. That Classname part is really inconvenient.

Upvotes: 1

Views: 366

Answers (1)

Heinzi
Heinzi

Reputation: 172478

You can make your life a little easier by specifying an alias for your class name:

using G = MyNamespace.MyClassContainingGlobalConstants;

...

var x = G.MyConstant;

Apart from that, I'm not aware of any C# feature that would allow you to access static class members without some kind of class name. (BTW, VB.NET has Modules for this purpose.)

Upvotes: 4

Related Questions