Find the output of C#.Net programs | Enumeration | Set 1: Enhance the knowledge of C#.Net Enumeration concepts by solving and finding the output of some C#.Net programs.
Question 1:
using System;
namespace Demo
{
enum Colors{
RED,GREEN,YELLOW,WHITE
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Colors C1 = Colors.YELLOW;
Colors C2 = Colors.GREEN;
int C = 0;
C = C1 + C2;
Console.WriteLine("C: " + C);
}
}
}
Question 2:
using System;
namespace Demo
{
enum Colors{
RED,GREEN,YELLOW,WHITE
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Colors C1 = Colors.YELLOW;
Colors C2 = Colors.GREEN;
int C = 0;
C = (int)C1 + (int)C2;
Console.WriteLine("C: " + C);
}
}
}
Question 3:
using System;
namespace Demo
{
enum Colors{
RED,GREEN=2,YELLOW,WHITE
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Colors C1 = Colors.RED;
Colors C2 = Colors.WHITE;
int C = 0;
C = (int)C1 + (int)C2;
Console.WriteLine("C: " + C);
}
}
}
Question 4:
using System;
namespace Demo
{
public enum Students
{
STUDENT1 = "Rahul", STUDENT2 = "Rohit", STUDENT3 = "Virat",STUDENT4="Shikhar"
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
string STR = "";
STR = (string)Students.STUDENT1 + (string)Students.STUDENT2;
Console.WriteLine(STR);
}
}
}
Question 5:
using System;
namespace Demo
{
public enum FloatVals
{
VAL1=10.2F,VAL2=11.3F,VAL3=12.5F
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
float VAL = 0.0F;
VAL = (float)FloatVals.VAL1 + (float)(FloatVals.VAL3);
Console.WriteLine(VAL);
}
}
}
Answer 1:
Output:
Explanation:
The above program will generate syntax error because we cannot add two enumeration operands using plus '+' operator directly.
Answer 2:
Output:
Explanation:
In the above program, we created a Colors enumeration that contains RED, GREEN, YELLOW, WHITE constants. Here, the first constant is initialized with 0 and every next constant increased by 1 automatically.
There are the following values contained by above enum constants:
RED = 0, GREEN=1, YELLOW=2, WHITE=3
Now come to the Main() method, here we create two enum variables C1 and C2 that are initialized with YELLOW and GREEN respectively. Then we created an integer variable C initialized with 0.
C = (int)C1 + (int)C2;
In the above statement, we typecast C1 and C2 into integer and add both constant and assigned to C.
C = 2 + 1; C = 3;
And then finally we printed the value of variable C on the console screen.
Answer 3:
Output:
Explanation:
In the above program, we created a Colors enumeration that contains RED, GREEN, YELLOW, WHITE constants. Here, the first constant is initialized with 0 and every next constant increased by 1 automatically.
But we assigned 2 to the GREEN then YELLOW and WHITE will be 3 and 4 respectively.
There are the following values contained by above enum constants:
RED = 0, GREEN=2, YELLOW=3, WHITE=4
Now come to the Main() method, here we create two enum variables C1 and C2 that are initialized with YELLOW and GREEN respectively. Then we created an integer variable C initialized with 0.
C = (int)C1 + (int)C2;
In the above statement, we typecast C1 and C2 into integer and add both constant and assigned to C.
C = 0 + 4; C = 4;
And then finally we printed the value of variable C on the console screen.
Answer 4:
Output:
Explanation:
The above program will generate syntax errors because an enum cannot contain string constant.
Answer 5:
Output:
Explanation:
The above program will generate syntax errors because an enum cannot contain string constant.