Find the output of C#.Net programs | static Keyword | Set 2: Enhance the knowledge of C#.Net static Keyword concepts by solving and finding the output of some C#.Net programs.
Question 1:
using System;
namespace Demo
{
class Sample
{
static int VAL;
public static Sample()
{
VAL=10;
}
public void Print()
{
Console.WriteLine(VAL);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Sample S = new Sample();
S.Print();
}
}
}
Question 2:
using System;
static namespace Demo
{
class Sample
{
int VAL;
public Sample()
{
VAL=10;
}
public void Print()
{
Console.WriteLine(VAL);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Sample S = new Sample();
S.Print();
}
}
}
Question 3:
using System;
namespace Demo
{
class Sample
{
int VAL;
public Sample()
{
VAL=10;
}
static ~Sample()
{
Console.WriteLine("Destructor called");
}
public void Print()
{
Console.WriteLine(VAL);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Sample S = new Sample();
S.Print();
}
}
}
Answer 1:
Output:
Explanation:
The above program will generate syntax error because we cannot use access modifiers with static constructors in C#.
Answer 2:
Output:
Explanation:
The above program will generate syntax error here we tried to create the static namespace, which is not possible. We cannot create a static namespace in C#.
Answer 3:
Output:
Explanation:
The above program will generate syntax error because we cannot define static destructor in C#.
need an explanation for this answer? contact us directly to get an explanation for this answer