Class Flashcards
What is a constructor?
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.
What are object initializers?
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" };
What is params keyword?
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 many access modifiers available?
public private protected internal ProtectedInternal
code snippet for adding a ne properties
prop + tab Tab
What is the parent of all classes in .Net?
Object class
What is Class Coupling
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.
What is Inheritance?
- 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
{
}
why should we favor composition over inheritance?
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
Naming convention for private fields?
use _
What is boxing?
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.
Realtime example og boxing?
var list = new ArrayList();
list.Add(object);
Add method accepts object.
list.Add(1) will box the value.
If e create one of the meber as Private, how can we access the property?
Create a method called Get and Set***()
What is the use? The use is that members should not be easily accessible.
How can we pass from one constructor to an another constructor?
using this..
What is readonly keyword?
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.