Home > Net >  Can I declare a record positional parameter with a type defined inside the record?
Can I declare a record positional parameter with a type defined inside the record?

Time:10-08

Is it possible for record to be declared with positional parameters that include a type defined inside the record definition?

For example, can a variation of the following record declaration be made valid without moving the enum out of the record?

internal record class Character(string Name, GenderEnum Gender, int Age)
{
    public enum GenderEnum : byte { Male, Female, Other }
}

CodePudding user response:

This would follow the same rules as a type within a class. You need to fully qualify the type.

internal record class Character(string Name, Character.GenderEnum Gender, int Age)
{
    public enum GenderEnum : byte { Male, Female, Other }
}
var character = new Character("Joe", Character.GenderEnum.Male, 55);
Console.WriteLine(character);
// Character { Name = Joe, Gender = Male, Age = 55 }
  • Related