C#.Net find output programs (Enumeration) | set 2

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.
Submitted by Nidhi, on February 07, 2021

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);
        }
    }
}

Output:

22
Press any key to continue . . .

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.

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);
        }
    }
}

Output:

main.cs(10,12): error CS1525: Unexpected symbol `enum'
main.cs(11,12): error CS1525: Unexpected symbol `{'
main.cs(12,31): error CS1525: Unexpected symbol `,', expecting `;' or `}'
main.cs(12,47): error CS1525: Unexpected symbol `,', expecting `;' or `}'
main.cs(13,13): error CS1002: ; expected

Explanation:

The above program will generate syntax errors because we cannot define an enum inside the method of a class.

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;
            }
        }
    }
}

Output:

Tata Car
Press any key to continue . . .

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.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.