Kwizcom Forms

Enum in C#.Net

In C#.net tutorial, we will discuss Enum in C#.Net and how to set enum values with spaces in c#.net?

Enums in C#.Net

The enum keyword is used to declare an enumeration in c#.net, a distinct type consisting of a set of named constants called the enumerator list. Every enumeration type has an underlying type, which can be any integral type except char.

SharePoint deveopment training course

The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.

we can declare an enum in c#.net like below:

enum WesiteList
{
AspDotNetHelp,
Fewlines4Biju,
OnlySharePoint2013,
SharePoint_Net,
EnjoySharePoint
}

Here in this enumeration AspDotNetHelp is o, Fewlines4Biju is 1 etc.

But you can initialize enumeration like below:

enum WesiteList
{
AspDotNetHelp=1,
Fewlines4Biju,
OnlySharePoint2013,
SharePoint_Net,
EnjoySharePoint
}

Here in this enumeration AspDotNetHelp is 1, Fewlines4Biju is 2 etc.

We can get the value like below:

int website = (int) WebsiteList.AspDotNetHelp;

.Net class library uses Enumeration hugly. Enums are value types and are created on the stack and not on the heap.

How to set enum values with spaces in c#.net?

Now, let us see how to set enum values with space in C#.Net.

If you will give space and declare like below then it will give an error.

public enum MonthYear
   {
       Jan 2014 = 0,
}

Here the error will come because of space between Jan 2014. So below is the syntax to declare enum values with space.

public enum MonthYear
   {
       [EnumMember(Value = “Jan 2014”)]
       Jan2014 = 0,
       [EnumMember(Value = “Feb 2014”)]
       Feb2014 = 0,
}

Hope this C#.Net tutorial helps to learn C#.Net enums and how to set enum values with spaces in c#.net?

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →