Enum.GetName
时间: 2023-03-22 12:00:27 浏览: 138
`Enum.GetName` is a method in C# programming language that is used to get the name of a constant in an enumeration.
The method takes two parameters: the type of the enumeration and the value of the constant whose name you want to retrieve. The method returns a string that represents the name of the constant.
Here's an example of how to use `Enum.GetName`:
```csharp
enum DaysOfWeek
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
DaysOfWeek day = DaysOfWeek.Tuesday;
string name = Enum.GetName(typeof(DaysOfWeek), day);
Console.WriteLine(name); // Output: "Tuesday"
```
In this example, we have an enumeration called `DaysOfWeek` with seven constants representing the days of the week. We assign the value `DaysOfWeek.Tuesday` to the `day` variable, and then call `Enum.GetName` method to retrieve the name of the `Tuesday` constant in the `DaysOfWeek` enumeration. The returned string is then printed to the console.
阅读全文