Pass in an enum as a method parameter

前端 未结 4 1698
北海茫月
北海茫月 2020-12-30 18:52

I have declared an enum:

public enum SupportedPermissions
{
    basic,
    repository,
    both
}

I also have a POCO like this:

<         


        
4条回答
  •  余生分开走
    2020-12-30 19:37

    If you want to pass in the value to use, you have to use the enum type you declared and directly use the supplied value:

    public string CreateFile(string id, string name, string description,
                  /* --> */  SupportedPermissions supportedPermissions)
    {
        file = new File
        {  
            Name = name,
            Id = id,
            Description = description,
            SupportedPermissions = supportedPermissions // <---
        };
    
        return file.Id;
    }
    

    If you instead want to use a fixed value, you don't need any parameter at all. Instead, directly use the enum value. The syntax is similar to a static member of a class:

    public string CreateFile(string id, string name, string description) // <---
    {
        file = new File
        {  
            Name = name,
            Id = id,
            Description = description,
            SupportedPermissions = SupportedPermissions.basic // <---
        };
    
        return file.Id;
    }
    

提交回复
热议问题