user1961169
user1961169

Reputation: 799

C# can I access an enum without full qualified names

I have a C# enum type that ends up with very long qualified names. e.g.

DataSet1.ContactLogTypeValues.ReminderToFollowupOverdueInvoice.

For readability, it would be nice if I could tell a particular function to just use the last portion of the name, something like...

{
    using DataSet1.ContactLogTypeValues;
    ...
    logtype = ReminderToFollowupOverdueInvoice;
    ...
}

Is it possible to do anything like this in C#?

Upvotes: 11

Views: 4993

Answers (3)

Since C# 6, you can use using static:

using static DataSet1.ContactLogTypeValues;
...
logtype = ReminderToFollowupOverdueInvoice;
...

See https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive#static-modifier for more details.

Upvotes: 16

Jason
Jason

Reputation: 1383

I realize this might not be the solution you envisioned, but it does allow you to write the code you are requesting.

enum ContactLogTypeValues
{
    ReminderToFollowupOverdueInvoice,
    AnotherValue1,
    AnotherValue2,
    AnotherValue3
};

static ContactLogTypeValues ReminderToFollowupOverdueInvoice = ContactLogTypeValues.ReminderToFollowupOverdueInvoice;
static ContactLogTypeValues AnotherValue1 = ContactLogTypeValues.AnotherValue1;
static ContactLogTypeValues AnotherValue2 = ContactLogTypeValues.AnotherValue2;
static ContactLogTypeValues AnotherValue3 = ContactLogTypeValues.AnotherValue3;

static void Main(string[] args)
{
    var a = ReminderToFollowupOverdueInvoice;

}

Upvotes: 3

Nikolai Samteladze
Nikolai Samteladze

Reputation: 7797

You can use using directive to specify an alias. It will exist everywhere in the file, not in one particular method though.

Upvotes: 5

Related Questions