Part 3 Flashcards

1
Q

What is object initializers?

A

It is a syntax used to initialize an object, without a need to call one of its constructors.

Compiler first calls the default constructor, then it initializes the members specified in the object initializers.

for example,

class Person
{
   int Id,
   string name;
   string location;
}

this class can have combination of following constructors:

Person(int id)
{
this.id = id
}

Person(int id, string name)
{
   this.id = id;
   this.name = name;
}
Person(int id, string name, string location)
{
   this.id = id;
   this.name = name;
   this.location = location;
}

To avoid having such multiple constructors, we can have object initializers as:

var personObj = new Person {
   id = 1,
   name = "Bharath"
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Is-A and Has-A relationships?

A

Classes can have Is-A or Has-A relationships.

Is-A relationship is achieved using Inheritance. Example, Person is-a Animal.

Has-A relationship is achieved using Composition.
Example, Car Has-A engine.

Code example:

class Animal
{
   string name;
   string eat;
}

Class Person: Animal
{
string think;
}

For compostion:

class Wheel
{
   int dimention;
   int width;
}

Class Car
{
private readonly Wheel _wheel;

Car(Wheel wheel)
{
_wheel = wheel;
}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which one to better: Inheritance or omposition?

A

Depends on design:

Pros of Inheritance:
Code Re-use
Easy to understand.

Cons of Inheritance:
Tightly coupled.

For example:

Consider a class, say Animal. It has properties eat, sleep. We will derive two classes from Animal: Dog, person, Fish. Now, if we add, walk property to Animal, the design breaks because does not walk. This has made the classes tightly coupled.

Using composition, Dog and Person Has-a Animal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are access modifiers in C#?

A

There are 5 types of access modifiers:

a. Public: When we mark members as public. we can use those members anywhere outside the class.
b. Private: These members cannot be accessed outside the class.
c. Protected: Only derived classes can access the members.
d. Internal: Usually classes are marked as Internal and this makes the class only available inside an assembly and not accessible outside other assesmblies.
e. Protected-Internal: Only available to the derived classes and inside the assembly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Consider this:

class vehicle
{
   string regN;
   vehicle(string regNumb)
   {
      regN = regNumb
    }
}

Class car : vehicle
{

Car()
{
}

}

Car carObj = new Car();

Will this work?

A

No. Vehicle has a parameterised constructor and it expects car to call the parameterised construcot.

class vehicle
{
   string regN;
   vehicle(string regNumb)
   {
      regN = regNumb
    }
}

Class car : vehicle
{

Car(string reg) : base(reg)
{
}

}

Car carObj = new Car(“CA”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is upcasting and DOwncasting?

A

When we convert derived class to base class, it called upcast. base to derived is called downcast.

Upcast is implicit while the DOwncast is not implicit.

if shape is a base class and circle is a derived class:

Upcast:

Circle c = new Circle()

Shape s= c;

Shape s2 = new Shape();
Circle c2 = (Shape)s2;

But if the downcast failes, we will get an exception. Hence it is better to use as

Circle c2 = s2 as Shape;

as keyword return null.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which is the base class of all classes in .Net?

A

Object class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is boxing and unboxing?

A

A process of converting value type to the reference type is called boxing.

int i = 10;
object j = i;

During boxing, the value type is boxed and stored it in a heap. and the object will point it to the reference.

object i =10l
int j = (int) i;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly