Question-1: What is C#? 

Answer: C# is a computer programming language developed by Microsoft in 2000. Using C# language we can develop different types of applications such as web applications, mobile applications, windows applications, etc. 


Question-2: What are Managed and Unmanaged Code? 

Answer

    Managed Code: Code that is written within a framework could be called managed code because our framework will manage application files, memory allocation and deallocation of objects and Code written in the framework is ready to run in CLR

    Unmanaged Code: Code written outside the framework could be called as unmanaged code because we need to manage everything in unmanaged code such as memory allocation, garbage collection etc.

 


Question-3: What are Boxing and Unboxing  

AnswerThe process of converting a value type to a reference type is known as Boxing. Boxing is an implicit conversion and our compiler can easily do boxing without any error.

 The process of converting reference type to value type is known as Unboxing. Unboxing is an explicit conversion because we had to manually convert the reference type to value type. 

 


Question-4: What is the difference between Throw and Throw ex 

AnswerBoth Throw and Throw ex is used to generate the exception in the program, using only throw which preserves the original exception stack trace whereas using Throw ex will overwrite the exception stack trace and also overwrite the original exception detail.  

Using Throw ex we can also manipulate the original exception and can produce our own exception as per our requirement. 


Question-5: What is the difference between IEnumerable and IQuerable  

Answer:

  • IEnumerable is part of Systm.collections namespaces whereas IQuerable is part of System.Linq namespace 
  • IEnumerable first loads the data into the memory and then performs the action, means IN-Memory where as IQuerable will perform the operation on the server side means OUT-Memory operation. 
  • IQuerable is suited when we have data over server-side such as querying DB and IEnumerable is suited when we are querying system objects such as Array, List etc 


Question-6: Explain the difference between value types and reference types. 

Answer: Value Type: In value type variable the actual values of the variables are saved at the memory address. So if the application searches for the value of the variable then it can get it on the actual location of the variable. 

Reference Type: In reference type variables the variables hold the memory address of the location where actual variable data is present. So if the application searches for the value of the variable, then instead of the value of the variable it will return the memory address of the location where actual values are stored. CLR does reference type allocation for variables when the data of the variable is large. 

Example: Value types are int, decimal, char, etc. Reference types are objects, Array, etc.

Question-7: What is the purpose of the 'var' keyword?

Answer: var keyword can be used in the places where we are not sure of the datatype of the variable. 'var' type variable can hold any type of data init. But once var keyword is initialized than the datatype of the variable is set to the first passed value type and from the next time the variable can only hold that type of data only. 

Example: var keyword can be used to initialize the variable that is holding the return value of any method but you are not sure what type of value will be returned by that function.

Example: Value types are int, decimal, char, etc. Reference types are objects, Array, etc.

Question-8: What is the difference between const and readonly?

Answer: Const: Once a value is assigned to the variable is initialized with const keyword, then we cannot change it through the application. We need to assign the value to the variable while initializing the variable. 

Readonly: Once a value is assigned to the variable is initialized with readonly keyword, then we cannot change it through the application. But we initialize the variable without assigning value to it, so if your variable is readonly type you can initialize a variable, and later when you assign any value to that variable then you cannot change it throughout the application. 

Example: const is used where you are certain of the variable at compile time and readonly is used where you are not certain of the variable value at compile time but the value will be assigned to the readonly at runtime and once value is assigned than variable cannot hold any other value init. 

Question-8: Explain the difference between == and .Equals()?

Answer

'==' Operator:
  • '==' is a operator that is used to compare two variables. It checks for equality and returns true if values are equal and false if not. 
  • We can overload the default behavior of the operator by operator overload. 
  • For the reference type variable, it checks for the reference instead of the actual value and returns true if both variables point to the same memory location. 
'.Equals()' Method:
  • '.Equal()' is a method that is used to compare two variables. It checks for equality and returns true if values are equal and false if not. 
  • We can override the default behavior of the method by method overriding. 
  • For the reference type variable, it checks for the reference instead of the actual value and returns true if both variables point to the same memory location. But this default behavior can be changed by overriding the method. 
Conclusion
Uses '==' when you have to perform a quick reference check only. and use Equal() when you want the content-based comparison.