Verbatim variable
Special chars don’t need to be escaped, since you informed the compiler to expect special characters, and to ignore them. A common use case might be to specify a connection string:
string sqlServer = @"SERVER01\SQL";
?? Operator
Null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand
int.parse vs convert.toint
Convert.ToInt32
is for dealing with any object that implements IConvertible
and can be converted to an int
. Also, Convert.ToInt32
returns 0
for null
, while int.Parse
throws a ArgumentNullException
.
int.Parse
is specifically for dealing with strings.
As it turns out, the string
type’s IConvertible
implementation merely uses int.Parse
in its ToInt32
method.
So effectively, if you call Convert.ToIn32
on a string
, you are calling int.Parse
, just with slightly more overhead (a couple more method calls).
This is true for any conversion from string
to some primitive type (they all call Parse
). So if you’re dealing with strongly-typed string
objects; Parse
it’s more direct.
params
By using the params
keyword, you can specify a method parameter that takes a variable number of arguments.
public static void ParamsMethod(params int[] Numbers
The above function can be invoked by
ParamsMethod() // Without any arguments
ParamsMethod(Numbers) //an array
ParamsMethod(1,2,3,4,5) // the arguments seperated by commas
Method hiding (Shadowing)
public class BaseClass
{
public void WriteNum()
{
Console.WriteLine(12);
}
}
public class DerivedClass : BaseClass
{
public new void WriteNum()
{
Console.WriteLine(42);
}
}
BaseClass isReallyBase = new BaseClass();
BaseClass isReallyDerived = new DerivedClass();
DerivedClass isClearlyDerived = new DerivedClass();
isReallyBase.WriteNum(); // writes 12
isReallyDerived.WriteNum(); // writes 12
isClearlyDerived.WriteNum(); // writes 42
Using new keyword to hide base class method.
Note:Using Base class object will invoke base class method
Hiding protects against a subsequent base class modification.
Overriding
public class BaseClass
{
public virtual void WriteStr()
{
Console.WriteLine("abc");
}
}
public class DerivedClass : BaseClass
{
public override void WriteStr()
{
Console.WriteLine("xyz");
}
}
/* ... */
BaseClass isReallyBase = new BaseClass();
BaseClass isReallyDerived = new DerivedClass();
DerivedClass isClearlyDerived = new DerivedClass();
isReallyBase.WriteStr(); // writes abc
isReallyDerived.WriteStr(); // writes xyz
isClearlyDerived.writeStr(); // writes xyz
Using override keyword to override virtual base class method.
Note: Derived object class will always will invoke derived class method. Overriding is a type of polymorphism (in which method behaviour will difffer depending on the objects type at runtime)
Overriding achieves polymorphism
Struct vs Class
Struct
Struct – value type (stack), public members by default, no inheritance , no polymorphism, no declared parameterless contructor, no declared destructor
The reason value types can’t support inheritance is because of arrays https://stackoverflow.com/questions/1222935/why-dont-structs-support-inheritance
Interface vs Classs
Interface – only singatures no implementation, multiple inheritance possible, only inherit from other interfaces only.
Explicit interface implementation
When a class inherits from two interface which both of those interfaces have the method with same signature, there is a an ambiguity, to solve this typecast the class object to point to the method we want. See part 31 C# Tutorial – Venkat.
Abstract class vs Interface
Abstract class can have method implementations, can have variables (fields), No multiple inheritance since it is a class.
Type safe
Type-safe code accesses only the memory locations it is authorized to access. Consider this simple C# code below:
int mynumber = 3;
string mystring = “3”;
int result = mynumber + mystring;
The code above will not compile or run, displaying an error like Cannot implicitly convert type ‘string’ to ‘int’.
Delegate
A delegate is a type safe function pointer. Delegate are reference types.
Extension Methods
Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
Clone() vs CopyTo()
The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element’s object, resulting in a different, yet identical object.
So the difference are :
1- CopyTo require to have a destination array when Clone return a new array.
2- CopyTo let you specify an index (if required) to the destination array.
Polymorphism in C#
The ability of a programming language to process objects in different ways depending on their data type or class is known as Polymorphism. There are two types of polymorphism
- Compile time polymorphism. Best example is Overloading
- Runtime polymorphism. Best example is Overriding
Some advanced QandAs: http://www.geekinterview.com/Interview-Questions/Microsoft/C-Sharp/page1