Q:- What is difference between const and readonly fields?
Ans:- Constant is used to declare to a variables whose value cannot be changed. Readonly also serves the same purpose but the value is not known until the runtime. Suppose we want to perform some calculation before we assign the value to variable. It is not possible in case of const. The rule to assign value to readonly variable is that it should be assigned inside the constructor not anywhere else. Also it is possible t0 have non-static readonly field in a class means for each instance of a class readonly field can have different value. Unlike const field, if we want a readonly field to be static, we have to declare it as such.
Q:- What is Anonymous Type?
Ans:- An anonymous type is simply a nameless class that inherits from object. Anonymous types can be created with the use of var variable. Declaration of var look like this:
var captain = new {FirstName = “Ron”, LastName = “Jones”};
This would create a new object with FirstName and LastName properties. The actual type of these new objects is unknown.
Q:- What is difference between Structs and Classes?
Ans:-
1. Structs are value type and classes are reference type.
2. Instance of classes stored in managed heap whereas structs stored either in stack or inline (if they are part of another object that is stored in heap).
3. Structs do no support inheritance whereas classes do.
4. Structs has default no-parameter constructor, which we are not allowed to replace. In classes we can have any constructor.
5. Classes are derived from System.Object (if base class is not specified) whereas a struct is always derived from System.ValueType. which in turn derives from System.Object.
6. Structs can be instantiated without using the new operator but classes can’t.
class TheClass
{
public int x;
}
TheClass theClass; //object is declared but not initialized
theClass.x = 10; // throw exception
struct TheStruct
{
public int x;
}
TheStruct theStruct; //default constructor is called
theStruct.x = 10;
Q:- What are Extension Methods?
Ans:- Extension methods are a way to extend the functionality of a class in cases source code is not available. Otherwise, inheritance is the great way to add functionality to objects. Extensions methods are static methods that can appear to be part of class without actually being in the source code of class.
Example:- Assume we have a class Money need a method AddToMoney(int amountToAdd). To do this create a static class and add the AddToMoney method as a static method. Here is what the code would look like:
public static class MoneyExtension
{
public static void AddToMoney(this Money money, int amountToAdd) //notice the first parameter
{
money.Amount += amountToAdd;
}
}
First parameter is the type that is being extended preceded by the this keyword. This is what tells the compiler that this method is part of Money type. To use the new method, we can make call just like any other method:
Money money = new Money(); //even though the extension method is static, standard instance method is used
money.AddToMoney(100); //notice first parameter doesn't appear
Q:- What are Nullable Type variables?
Ans:- Nullable types can represent all the values of an underlying type, and an additional nullvalue. Nullable types are declared in one of two ways:
System.Nullable variable
-or-
T? variable
T is the underlying type of the nullable type. T can be any value type including struct; it cannot be a reference type.
For an example of when you might use a nullable type, consider how an ordinary Boolean variable can have two values: true and false. There is no value that signifies “undefined”. In many programming applications, most notably database interactions, variables can exist in an undefined state. For example, a field in a database may contain the values true or false, but it may also contain no value at all. Similarly, reference types can be set to null to indicate that they are not initialized.
int? x = 10;
if (x.HasValue)
{
System.Console.WriteLine(x.Value);
}
else
{
System.Console.WriteLine("Undefined");
}
Explicit Conversion: A nullable type can be cast to a regular type, either explicitly with a cast, or by using the Value property.
Implicit Conversion: The conversion from an ordinary type to a nullable type, is implicit.
int? n = null;
//int m1 = n; // Will not compile.
int m2 = (int)n; // Compiles, but will create an exception if x is null.
int m3 = n.Value; // Compiles, but will create an exception if x is null.
n = 10; // Implicit conversion.
The ?? (Null Coalescing) Operator: The ?? operator defines a default value that is returned when a nullable type is assigned to a non-nullable type. This operator can also be used with multiple nullable types.
int? c = null;
int? e = null;
// d = c, unless c is null, in which case d = -1.
int d = c ?? -1;
// g = e or c, unless e and c are both null, in which case g = -1.
int g = e ?? c ?? -1;
Q:- What is is and as Operator?
Ans:- The is operator checks whether an object is compatible with the given type or not and returns a boolean result. The object may either be of same type or may be derived from that particular given type.
The as operator converts the reference types explicitly into a specific type. If the reference type is compatible with the specified type, it performs the successful conversion; otherwise, it returns the null vlaue. Note as operator doesn’t throw an exception.
object i = 25;
bool bFlag = i is string; //return false as i is of integer type
object str = "Hello World";
string str1 = i as string; //return null as casting i to string fails
string str2 = str as string; // return Hello World as successful casting str to string type