Krystyna Ślusarczyk Junior Level Flashcards
What is a difference between class and a struct?
a. Struct is a value type, and class is a reference type.
b. Struct cannot have a destructor. THe reason is that if we have a struct whihc manages database and in destrucor we close the database. and when we create a copy of this struct when we pass this as a param, the copied struct will close the connection.
c. Struct has an implicit paramaetrised constreucotr and all the properties should be assigned in the constructor.
d. Struct cannot have parameterless constructor.
e. Struct are sealed (all valuetypes).
f. Struct
What is a base type for struct?
System.ValueType
Is it possible to inherit from a struct?
No
What is a Partial class?
Partial classes are classes which are split over two or more source files. WHen the applciation is compiled these classes are combined to one class. We can have partial class, interface, strut and methods.
Exmaple.
partial class person {
}
partial class Person {
}
What is the use of partial classes?
Partial classes are used to split one class into many classes which is useful when many developers contribute t the same class and merging these classes is difficult in source control/repo. Another use is that when the system generates a class then if we are too contributing to the same class, then we can use partial class.
What does new keyword do?
3 uses:
a. new operator: to invoke a constructor to create a new object
b. new modifier: explicitly hide a member of a base class in the derived class.
c. new constraint: type argument in the generic class must have a parameter-less constructor
class generic where T: new() { get() { return new T(); } }
we don’t know if T will have parameter-less constructor.
What is the purpose of static keyword?
2 contexts:
Static Modifiers: Used to declare a class as struct as well as static members of a class.
Static data member can be used in non-static methods.
Static function cannot use non-static members.
Static modifiers belong a type/class and not to a object.
all const members are static.
WE CANNOT HAVE A STATIC STRUCT AND RECORDS.
Static Directives: they can be used to reference a static members without explicitly specifying their name eveytime.
Example, we dont have to use Console.WriteLine everutim if we use
using static System.COnsole.
we can just use WriteLine.
What is a static class?
Static class is a class that cannot be instantiated and can only contain static members.
It can work as a container for static members that work just on input parameters and do not have to get or set instance members.
Example, if we have a calculator calss, we can make it as static. we don’t have to create multiple instances of calculator. Ifwe have different elements for difference instance then we can have non-static class.
Name some static class?
System,.Math
System.Console
Systm.Environemnt
Can we derive from a static class?
No. Static classes are sealed.
Can a static class has a constructor?
yes, it can only have staic constructor.
Where to initialize static memebers ?
In a static constructor.
can a non-static class have a static constructor?
Yes, it will be used to initialize the static members.
Static constructor will be called before the instance constructor is invoked.
What is a static constructor?
They are used to initialize static members. They should be parameter-less. They cannot be inherited. They cannot have access modifers. They cannot be called directly. Only CLR can invoke it.
What is a ternary conditional operator?
It provides a shorter syntax for if-else statement.
ternary operator always returns a value. Hence, we cannot add come expression such as console.write line
var val = dogSize > 25 ? “big” : small
Can all ternary operator be translated to if-else?
yes, we can.
can all else-if be converted to ternery?
No. ternary can only return a value.
What is null coalescing and null conditional operators?
they allow to perform if the value is null..
variable ?? “this will be executed if variable is null”
if (variable == null)
{
return variable;
}
return “this will be executed if variable is null”
null coalescing assignment operator?
if (variable == null)
{
create a variable
}
variable.Add();
(variable ??= create a vraible).Add();
What is null conditonal operator?
if (vvarible != NULL)
{
variable.method();
}
variable?.method();
What is the use of ? in C#?
it is used in ternary operator, null coleascing, null conditional and null coleascing assignment operators.
What are extension methods?
An extension method is declared outside the class and the extension method can be called upon the objects as a regualr method.
Using EM, we can add a new methods to the exsiting class without having its source code, rebuilding the code or inheriting the class.
To cretae an extension method, we need to create a static class and statci method. the first parameter should be this.
static class stringExtension { static string newMethod(this string, anyOtherParam) {
}
}
If a method i defined in extension class and the method is also available in the original class. Which method will be called?
The original method will be called.
How would you add a new funcitonality to an exsiting class without modifying it?
Extension methods.
Can we add extension method to System.Object?
Yes
What is nullable tyepes?
Nullable types are any type can be assigned a value of NULL.
By default reference can be assigned NULL but value types cannot be assigned null.
The reason we need nullable is that sometimes in DB we may have null values
Example:
Nullable i = null;
Nullable has provided two functions:
HasValue : this property will check if the var is null.
Value: this property will return the value.
We cannot use it like normal int.