Home »
C#.Net »
C#.Net find output programs
C#.Net find output programs (const Keyword) | set 1
Find the output of C#.Net programs | const Keyword | Set 1: Enhance the knowledge of C#.Net const Keyword 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
{
const int A = 10;
//Entry point of the program
static void Main(string[] args)
{
int C;
const int B;
B = 20;
C = A + B;
Console.WriteLine(C);
}
}
}
Output:
ain.cs(13,24): error CS0145: A const field requires a value to be provided
Explanation:
The above program will generate syntax error because constant B must be initialized at the time of declaration. But we assigned 20 after its declaration.
Question 2:
using System;
namespace Demo
{
class Program
{
const int A = 10;
const int B;
Program():B(20)
{
}
//Entry point of the program
static void Main(string[] args)
{
int C;
C = A + B;
Console.WriteLine(C);
}
}
}
Output:
main.cs(8,20): error CS0145: A const field requires a value to be provided
main.cs(10,18): error CS1525: Unexpected symbol `B', expecting `base' or `this'
Explanation:
The above program will generate syntax errors because, in the above program, we tried to initialize const data members using the constructor.
Program():B(20)
{}
But we did not use the correct way to initialized const data members. The above constructor definition looks like the member initializer list in C++, but it is not supported in C#.
Question 3:
using System;
namespace Demo
{
class Program
{
const void Fun()
{
Console.WriteLine("Fun() called");
}
//Entry point of the program
static void Main(string[] args)
{
Program P = new Program();
P.Fun();
}
}
}
Output:
main.cs(7,15): error CS1547: Keyword `void' cannot be used in this context
main.cs(7,23): error CS0145: A const field requires a value to be provided
main.cs(12,15): error CS1525: Unexpected symbol `void', expecting `class',
`delegate', `enum', `interface', `partial', or `struct'
main.cs(12,32): error CS1514: Unexpected symbol `[', expecting `.' or `{'
main.cs(12,32): error CS1525: Unexpected symbol `]', expecting `class', `delegate',
`enum', `interface', `partial', or `struct'
main.cs(14,25): error CS1530: Keyword `new' is not allowed on namespace elements
main.cs(14,28): error CS1525: Unexpected symbol `Program', expecting `class',
`delegate', `enum', `interface', `partial', or `struct'
Explanation:
The above program will generate syntax errors because we cannot define a constant method in C#, here we defined method Fun() with const keyword, which is not correct.
Question 4:
using System;
namespace Demo
{
class Program
{
void Fun()
{
Console.WriteLine("Fun() called");
}
//Entry point of the program
static void Main(string[] args)
{
const Program P = null;
Console.WriteLine("Hello World");
}
}
}
Output:
Hello World
Press any key to continue . . .
Explanation:
The above program will print "Hello World" on the console screen. In the above program, we defined method fun() inside the Program class, and we created a const reference of Program class initialized with null and then we print "Hello World" on the console screen.
Question 5:
using System;
namespace Demo
{
class Program
{
void Fun()
{
Console.WriteLine("Fun() called");
}
//Entry point of the program
static void Main(string[] args)
{
const Program P = new Program();
P.Fun();
}
}
}
Output:
main.cs(14,31): error CS0133: The expression being assigned to `P'
must be a constant or default value
Explanation:
The above program will generate syntax error because a const reference except string can only be initialized with null, but here we created a const reference for Program class, which is not correct.