Find the output of C#.Net programs | Structure | Set 3: Enhance the knowledge of C#.Net Structure concepts by solving and finding the output of some C#.Net programs
Question 1:
using System;
namespace Demo
{
struct Sample
{
int A;
int B;
public static Sample(int A, int B)
{
this.A=A;
this.B=B;
}
public int Sum()
{
return A + B;
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
int C = 0;
Sample S= new Sample(10,20);
C = S.Sum();
Console.WriteLine(C);
}
}
}
Question 2:
using System;
namespace Demo
{
static struct Sample
{
int A;
int B;
public Sample(int A, int B)
{
this.A=A;
this.B=B;
}
public int Sum()
{
return A + B;
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
int C = 0;
Sample S= new Sample(10,20);
C = S.Sum();
Console.WriteLine(C);
}
}
}
Question 3:
using System;
namespace Demo
{
struct Sample
{
public static int NUM;
public static void Method()
{
NUM++;
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Sample.Method();
Sample.Method();
Console.WriteLine(Sample.NUM);
}
}
}
Question 4:
using System;
namespace Demo
{
struct Sample
{
public static int NUM;
public static void Method()
{
NUM++;
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Sample S;
S.Method();
S.Method();
Console.WriteLine(S.NUM);
}
}
}
Question 5:
using System;
namespace Demo
{
const struct Sample
{
public static int NUM;
public static void Method()
{
NUM++;
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Sample.Method();
Sample.Method();
Console.WriteLine(Sample.NUM);
}
}
}
Answer 1:
Output:
Explanation:
The above program will generate syntax errors because we defined a static constructor inside the structure, but a static constructor must be parameter less and without any access modifier.
Answer 2:
Output:
Explanation:
The above program will generate syntax error because we cannot use static keyword with a structure like this,
static struct Sample
Answer 3:
Output:
Explanation:
In the above program, we created a structure that contains a static member NUM and a static method Method(), the Method() is used to increase the value of static member NUM in every call. In the Main() method we called Method() two times and then print the value of NUM, then 2 will be printed on the console screen.
Answer 4:
Output:
Explanation:
The above program will generate syntax errors because we cannot static members of a structure using instance reference.
Answer 5:
Output:
Explanation:
The above method will generate syntax error because we used the const keyword in structure definition, which is not correct, we cannot use const keyword like this.
need an explanation for this answer? contact us directly to get an explanation for this answer