1- In case of inheritence, what is calls first?
Base Constructor is called first. But the initializer of fields in derived class is called first.
The calling order is
- derived class field initializer
- base class field initializer
- base class constructor
- derived class constructor
Let's say you have classes like this:
class A {}
class B : A {}
class C : B {}
First, field initializers will be called in order of most-derived to least-derived classes. So first field initializers of
C
, then B
, then A
.
The constructors will then be called in the opposite order: First
A
's constructor, then B
, then C
.public class BaseClass {
private string sentenceOne = null; // A
public BaseClass() {
sentenceOne = "The quick brown fox"; // B
}
}
public class SubClass : BaseClass {
private string sentenceTwo = null; // C
public SubClass() {
sentenceTwo = "jumps over the lazy dog"; // D
}
}
Answer: Order of execution is: C, A, B, D.
3- What will be the output of below Program.
using System;
class Base
{
public Base()
{
Console.WriteLine("BASE 1");
}
public Base(int x)
{
Console.WriteLine("BASE 2");
}
}
class Derived : Base
{
public Derived():base(10)
{
Console.WriteLine("DERIVED CLASS");
}
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
}
}
Answer:
BASE2
DERIVED CLASS
4- What will be the output of below Program.
using System;
class A
{
public A() {
PrintFields();
}
public virtual void PrintFields() {}
}
class B: A
{
int x = 1;
int y;
public B() {
y = -1;
}
public override void PrintFields() {
Console.WriteLine("x = {0}, y = {1}", x, y);
}
}
Answer:
x = 1, y = 0
5- What will be the output of below Program.
class Base
{
public Base()
{
Console.WriteLine("Inside Base
Constructor");
}
~Base()
{
Console.WriteLine("Inside Base Destructor");
}
}
class Derived : Base
{
public Derived()
{
Console.WriteLine("Inside The Derived
Constructor");
}
~Derived()
{
Console.WriteLine("Inside The Derived
Destructor");
}
}
Answer:
Inside Base Constructor
Inside The Derived Constructor
Inside The Derived Constructor
6- What will be the output of below Program.
Answer:
Inside base class parameterized constructor
Inside derived class parametrized constructor
7- Are C# constructors inherited?
Constructors are not inherited but they get executed because when you create an object of child class then first compiler will create a parent then comes to child and while creation / Execution of parent, Constructor will be called implicitly.
class Class1
{
protected int a, b;
public Class1()
{
a = 0;
b = 0;
Console.WriteLine("Inside base class default
constructor");
}
public Class1(int a, int b)
{
this.a = a;
this.b = b;
Console.WriteLine("Inside base class
parameterized constructor");
}
}
class Class2 : Class1
{
int c;
public Class2(int a, int b, int c) : base(a, b)
{
this.c = c;
Console.WriteLine("Inside derived class
parametrized constructor");
}
}
Answer:
Inside base class parameterized constructor
Inside derived class parametrized constructor
7- Are C# constructors inherited?
Constructors are not inherited but they get executed because when you create an object of child class then first compiler will create a parent then comes to child and while creation / Execution of parent, Constructor will be called implicitly.
What will be the output of below code
int a= 1;
int i,j ;
i = a++; //1
j = ++a; //3
Console.WriteLine(i+j);
Output: 4
int i, j = 1, k;
for (i = 0; i < 5; i++)
{
k = j++ + ++j;
Console.Write(k + " ");
}
Console.Read();
Output: 4 8 12 16 20
delegate void Printer();
static void Main()
{
List<Printer> printers = new List<Printer>();
int i = 0;
for (; i < 10; i++)
{
printers.Add(delegate { Console.WriteLine(i); });
}
foreach (var printer in printers)
{
printer();
}
Console.Read();
}
Learn More
https://dailydotnettips.com/
0 comments:
Post a Comment