Find the output of C#.Net programs | Structure | Set 2: 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
{
public int A;
public int B;
public void init()
{
A = 10;
B = 20;
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
int C = 0;
Sample S;
S.init();
C = S.A + S.B;
Console.WriteLine(C);
}
}
}
Question 2:
using System;
namespace Demo
{
struct Sample
{
public int A;
public int B;
public Sample()
{
A = 10;
B = 20;
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
int C = 0;
Sample S= new Sample();
C = S.A + S.B;
Console.WriteLine(C);
}
}
}
Question 3:
using System;
namespace Demo
{
struct Sample
{
int A;
int B;
public Sample(int a, int b)
{
A = a;
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 4:
using System;
namespace Demo
{
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 5:
using System;
namespace Demo
{
struct Sample
{
int A;
int B;
public Sample(int A, int B)
{
this.A=A;
this.B=B;
}
public int Sum()
{
return A + B;
}
public ~Sample()
{
Console.WriteLine("Destructor called");
}
}
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);
}
}
}
Answer 1:
Output:
Explanation:
The above program will generate syntax error, because if a structure contains a method then we need to use a new operator to create a reference of structure.
Answer 2:
Output:
Explanation:
The above program will generate syntax error because a structure cannot contain explicit default or no-argument constructor in C#.
Answer 3:
Output:
Explanation:
In the above program, we created a structure that contains two data members A and B and we also created a parameterized constructor and a method Sum().
The parameterized constructor is used to initialize data members A and B, and method Sum() is used to return the addition of both A and B.
In the Main() method, I created a reference of structure and initialized using the parameterized constructor.
Then call method Sum that will return the addition of 10 and 20, that is 30, assigned to local variable C and printed on the console screen.
Answer 4:
Output:
Explanation:
In the above program, we created a structure that contains two data members A and B and we also created a parameterized constructor and a method Sum().
The parameterized constructor is used to initialize data member A and B, In the parameterized constructor A and B are data member and parameters, then we used this object to differentiate data members from local arguments, and the method Sum() is used to return the addition of both A and B.
In the Main() method, I created a reference of structure and initialized using the parameterized constructor.
Then call method Sum that will return the addition of 10 and 20, that is 30, assigned to local variable C and printed on the console screen.
Answer 5:
Output:
Explanation:
The above program will generate syntax error because a structure cannot contain a destructor.
need an explanation for this answer? contact us directly to get an explanation for this answer