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

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

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

Output:

main.cs(18,17): error CS0019: Operator `+' cannot be applied to 
operands of type `Demo.Colors' and `Demo.Colors'

Explanation:

The above program will generate syntax error because we cannot add two enumeration operands using plus '+' operator directly.

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

Output:

C: 3
Press any key to continue . . .

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.

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

Output:

C: 4
Press any key to continue . . .

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.

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

Output:

main.cs(7,20): error CS0029: Cannot implicitly convert type `string' to `int'
main.cs(7,40): error CS0029: Cannot implicitly convert type `string' to `int'
main.cs(7,60): error CS0029: Cannot implicitly convert type `string' to `int'
main.cs(7,77): error CS0029: Cannot implicitly convert type `string' to `int'
main.cs(17,36): error CS0030: Cannot convert type `Demo.Students' to `string'

Explanation:

The above program will generate syntax errors because an enum cannot contain string constant.

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

Output:

main.cs(7,14): error CS0031: Constant value `10.2' cannot be converted to a `int'
main.cs(7,25): error CS0031: Constant value `11.3' cannot be converted to a `int'
main.cs(7,36): error CS0031: Constant value `12.5' cannot be converted to a `int'

Explanation:

The above program will generate syntax errors because an enum cannot contain string constant.





Comments and Discussions!

Load comments ↻





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