Tuesday, October 7, 2014

Accessing "enum" values in .NET

Accessing enum values are very interesting in this sense that if you directly call enum item then it will return the left part of equal (=) sign. If you convert the enum item into integer then you will get right part of equal (=) sign. Lets see an example.

public enum NeedRefreshDataSource
    {
        Yes = 1,
        No = 2
    }


Now,

if you write NeedRefreshDataSource.Yes then you will get - Yes which is NeedRefreshDataSource type value.

if you write NeedRefreshDataSource.yes.ToString() then you will get - "Yes" which is string type value.

if you write Convert.ToInt32(NeedRefreshDataSource.yes) then you will get - 1 which is integer type value.

if you write Convert.ToInt32(NeedRefreshDataSource.yes).ToString() then you will get - "1" which is string type value.




Happy Coding...