Class Flashcards

1
Q

What is a constructor?

A

It is a special type of function which is used when initialization of an object. It does not return any value.
Having a construct is an optional.

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

What are object initializers?

A

Object initializers is the easiest and fastest way to assign values of an object’s properties and fields. An object can be initialized without explicitly calling a class’s constructor.

consider a class

class Person{
   public int year;
   public string name;
   public string country;
 /*  public Person(name)
   {
      this.name = name;
    }
   public Person(name, country)
   {
       this.name = name;
       this.country = country;
   } */
}

The list of constructors will grow. Henc, using object initializers, we can avoid multiple constructor.

     var Person = new Person
            {
                year = 1986,
            name = "bharth",

            country = "canada"

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

What is params keyword?

A

By using params keyword we can pass multiple numbers of arguments to a function.

public int Add(params int[] numbers)
        {
            int ret = 0;
            foreach(var num in numbers)
            {
                ret += num;
            }
        return ret;
    }

Console.WriteLine(person.Add(1,8, 5, 7, 8, 5));

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

how many access modifiers available?

A

public private protected internal ProtectedInternal

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

code snippet for adding a ne properties

A

prop + tab Tab

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

What is the parent of all classes in .Net?

A

Object class

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

What is Class Coupling

A
A measure of how interconnected classes and subsystems are.
- The more coupled classes, the harder it is to change them. A change in one class may
affect many other classes.
- Loosely coupled software, as opposed to tightly coupled software, is easier to change.
- Two types of relationships between classes: Inheritance and Composition.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Inheritance?

A
  • A kind of relationship between two classes that allows one to inherit code from the
    other.
  • Referred to as Is-A relationship: A Car is a Vehicle
  • Benefits: code re-use and polymorphic behaviour.
    public class Car : Vehicle
    {
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

why should we favor composition over inheritance?

A

Problems with inheritance:
• Easily abused by amateur designers / developers
• Leads to large complex hierarchies
• Such hierarchies are very fragile and a change may affect many classes
• Results in tight coupling

  • Benefits of composition:
    • Flexible
    • Leads to loose coupling
  • Having said all that, it doesn’t mean inheritance should be avoided at all times. In fact,
    it’s great to use inheritance when dealing with very stable classes on top of small
    hierarchies. As the hierarchy grows (or variations of classes increase), the hierarchy,
    however, becomes fragile. And that’s where composition can give you a better design
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Naming convention for private fields?

A

use _

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

What is boxing?

A

The process of converting value type instance to an object type reference.

int number = 10;
object obj = number;

10 is stored in the heap.

int j = (int) obj

VR - > Boxing

This is moer of theory. In realworld, when we use ArrayList we are actually boxing it when we call add.
            var arrList = new ArrayList();
        arrList.Add(1);
        arrList.Add("list");

Although this provides flexibility, it affects performance.

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

Realtime example og boxing?

A

var list = new ArrayList();

list.Add(object);

Add method accepts object.

list.Add(1) will box the value.

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

If e create one of the meber as Private, how can we access the property?

A

Create a method called Get and Set***()

What is the use? The use is that members should not be easily accessible.

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

How can we pass from one constructor to an another constructor?

A

using this..

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

What is readonly keyword?

A

readonly increases the robustness of the code. It needs to be initialized either when we declare it or in the constructor. Once, initialized it cannot be changed.

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

How to initlialzie a field?

A
A field can be initialized in two ways: In a constructor, or directly upon declaration. The
benefit of initialising a field during declaration is that if your class has one or more
constructors, you’ll make sure that the field will always be initialised irrespective of
which constructor is going to be called.