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

Find the output of C#.Net programs | Data Types | Set 1: Enhance the knowledge of C#.Net Data Types concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 05, 2021

Question 1:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int Len = 0;
            int A = 100;

            Len = sizeof(int);
            Console.WriteLine("Len : " + Len);

            Len = sizeof(A);
            Console.WriteLine("Len : " + Len);
        }
    }
}

Output:

main.cs(16,26): error CS0246: The type or namespace name `A' could not be found. 
Are you missing an assembly reference?

Explanation:

The above program will generate syntax error because we can get the size of the built-in type using sizeof(), but we cannot find the size of a variable in C#. That's why error will be generated by the above program.

To find out the size of a variable we need to use Bitconverter class. The code is given below:

byte[] bytes = BitConverter.GetBytes(A);
int LEN = bytes.Length;

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            var Len = 0;
            var A = 100;

            byte[] bytes = BitConverter.GetBytes(A);
            Len = bytes.Length;
            Console.WriteLine("Len : " + Len);
        }
    }
}

Output:

Len : 4

Explanation:

In the above program, we declared two implicit type variables Len, A, which is initialized with 0, 100 respectively. Then we convert variable A into bytes using GetBytes() method of BitConverter class, then find the length using Length property.

Question 3:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            var Len = 0;
            var A = "Hello";

            byte[] bytes = BitConverter.GetBytes(A);
            Len = bytes.Length;
            Console.WriteLine("Len : " + Len);
        }
    }
}

Output:

main.cs(13,41): error CS1502: The best overloaded method match for 
`System.BitConverter.GetBytes(bool)' has some invalid arguments
/usr/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error)
main.cs(13,50): error CS1503: Argument `#1' cannot convert `string' 
expression to type `bool'

Explanation:

The above program will generate syntax error because we cannot use GetBytes() method of the BitConverter class to convert the string into bytes.

We can get length of specified string using below code,

var Len = 0;
var A = "Hello";

Len = A.Length;
Console.WriteLine("Len : " + Len);

Question 4:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            unsigned short A = sizeof(double);
            unsigned short B = 20;
            int    C = 0;

            C = A * B + 100;

            Console.WriteLine("C: " + C);
        }
    }
}

Output:

main.cs(10,21): error CS1525: Unexpected symbol `short'
main.cs(11,21): error CS1525: Unexpected symbol `short'

Explanation:

The above program will generate syntax error, because unsigned short is not a built-in data-type in C#. In C#, ushort is used for unsigned short.

Question 5:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            ushort A = sizeof(double);
            ushort B = 20;
            ushort C = 0;

            C = A * B + 100;

            Console.WriteLine("C: " + C);
        }
    }
}

Output:

main.cs(14,17): error CS0266: Cannot implicitly convert type `int' to `ushort'.
An explicit conversion exists (are you missing a cast?)

Explanation:

The above program will syntax error because the below expression will return an integer value,

C = A * B + 100;

If you want to assign the result of the above expression then we need to typecast the above expression in ushort. The correct expression is given below:

C = (ushort)(A * B + 100);




Comments and Discussions!

Load comments ↻





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