Find the output of C#.Net programs | Enumeration | Set 2: 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
{
public enum FloatVals
{
VAL1=(int)10.2F,VAL2=(int)11.3F,VAL3=(int)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);
}
}
}
Question 2:
using System;
namespace Demo
{
class Program
{
//Entry point of the program
static void Main(string[] args)
{
enum FloatVals
{
VAL1=(int)10.2F,VAL2=(int)11.3F,VAL3=(int)12.5F
}
float VAL = 0.0F;
VAL = (float)FloatVals.VAL1 + (float)(FloatVals.VAL3);
Console.WriteLine(VAL);
}
}
}
Question 3:
using System;
namespace Demo
{
enum Cars
{
HYUNDAI, MARUTI, TATA, AUDI
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Cars C = Cars.TATA;
switch (C)
{
case Cars.HYUNDAI:
Console.WriteLine("Hundai Car");
break;
case Cars.MARUTI:
Console.WriteLine("Maruti Car");
break;
case Cars.TATA:
Console.WriteLine("Tata Car");
break;
case Cars.AUDI:
Console.WriteLine("Audi Car");
break;
default:
break;
}
}
}
}
Answer 1:
Output:
Explanation:
In the above program, we created a FloatVals enumeration that contains VAL1, VAL2, VAL3 constants.
Here, we initialized constants by typecasting float values into integer because enumeration can contain integer values.
Now come to the Main() method, here we created a float variable VAL initialized with 0.0F.
VAL = (float)FloatVals.VAL1 + (float)(FloatVals.VAL3); VAL = (int)10.2F + (int)12.5F ; VAL = 10 +12; VAL = 22;
And then finally we printed the value of variable VAL on the console screen.
Answer 2:
Output:
Explanation:
The above program will generate syntax errors because we cannot define an enum inside the method of a class.
Answer 3:
Output:
Explanation:
In the above program, we created an enum Cars that contains constants HYUNDAI, MARUTI, TATA, and AUDI. In the Main() method we created a variable C of type enum Cars initialized with TATA constant, And we created a switch block that will execute "case Cars.TATA" and print "Tata Car" on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer