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.
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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
Answer 1:
Output:
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;
Answer 2:
Output:
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.
Answer 3:
Output:
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);
Answer 4:
Output:
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.
Answer 5:
Output:
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);
need an explanation for this answer? contact us directly to get an explanation for this answer