c#枚举数字转枚举
1) What is the correct output of given code snippets in C#.NET?
using System;
class program
{
enum emp_salary : int
{
emp1,
emp2,
emp4
}
static void Main(string[] args)
{
int sal = (int)emp_salary.emp2;
Console.WriteLine(sal);
}
}
Answer & Explanation
Correct answer: 2
1
The above code will print "1" on console screen.
2) What is the correct output of given code snippets in C#.NET?
using System;
class program
{
enum emp_salary : int
{
emp1,
emp2,
emp3
}
static void Main(string[] args)
{
emp_salary.emp1 = 10;
Console.WriteLine(emp_salary.emp2);
}
}
Answer & Explanation
Correct answer: 3
Syntax error
The above code will generate syntax error because the left-hand side must be a variable.
3) What is the correct output of given code snippets in C#.NET?
using System;
class program
{
enum emp_salary : int
{
emp1,
emp2,
emp3
}
static void Main(string[] args)
{
emp_salary emp = emp_salary.emp3;
Console.WriteLine(emp.GetType());
}
}
Answer & Explanation
Correct answer: 4
program+emp_salary
The above will class name with enum name.
4) Can we create an enum with a protected access modifier in C#.NET?
protected Color { Red=1, Green=2};
Answer & Explanation
Correct answer: 1
Yes
Yes, we can create an enum with a protected modifier.
5) Can we create an enum with a private access modifier in C#.NET?
private Color { Red=1, Green=2};
Answer & Explanation
Correct answer: 1
Yes
Yes, we can create an enum with a private modifier.
翻译自: https://www.includehelp.com/dot-net/csharp-enumeration-aptitude-questions-and-answers-5.aspx
c#枚举数字转枚举