0

In my code I have this:

        var wordForm = new WordForm()
        {
            WordId = word.WordId,
            Definition = webWordForm.definition,
            SourceId = webWordForm.sourceId,
            StatusId = EStatus.Download,
            PosId = WordToPosId(webWordForm.partOfSpeech)
        };

I declared the Enum:

public enum EStatus {
    NotSet   = 0,
    New      = 1,
    Download = 2,
    Edited = 3
}

and the statusID:

public int StatusId { get; set; } // StatusId

Do I need to cast this to an int and if so how do I do that?

Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

2 Answers2

2

Cast the Enum as

StatusId = (int)EStatus.Download
Chaitanya
  • 171
  • 1
  • 7
  • 2
    Instead of spoonfeeding, give her something more like how Enum to int casting works. How enum is basically int values under the fabric. etc – fahadash Jun 12 '16 at 04:22
1

An explicit cast is needed if type of enum is declared. But the default value for enum is int

Every enumeration type has an underlying type, which can be any integral type except char.

So an example for this is:

enum Days : byte {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

Days is declared as byte so an explicit cast is needed when assigning it to an int variable since the enum is declared as byte

int x = (int)Days.Sun;

However, an explicit cast is necessary to convert from enum type to an integral type.

So for your example there is no need to cast it since the underlying type of your enumeration is an int

This is from the documentation enum c#. Please take time to read API's.

starturtle
  • 699
  • 8
  • 25
Anonymous Duck
  • 2,942
  • 1
  • 12
  • 35