Basic C#

What is partial class in c#?
Instead of defining multiple class we could define a single partial class which will act as same. At the compilation time all partial will be merged and treated as one.


Public partial class Employee
{
     public void DoWork()
     {}
}


Public partial class Employee
{
      public void GoToLunch()
      {}
}


What is boxing and unboxing?


Boxing: Conversion of value type into object is called boxing.
int a = 20;
Object age = a;
or
Object age = (Object) a;


Unboxing: Conversion of object type into value type is called unboxing.
Object age = 20;
Int a = (Int32) age;


What is Stack and Heap memory in .net?
Stack: Initialization and assign value into  value type is goes to Stack memory.
Heap: Initialization of reference type and assignment goes to Heap memory;


Example 1
Int a = 5;
int b = a;


Stack
int a = 5;
int b = 5;


Example 2
int a = 5;
Employee emp =  new Employee();
emp.age=a;
int age =(int32) emp.age;


Stack
int a = 5;
Stack
emp (ref)
Heap
Employee emp
Heap
emp.age=5;
Stack
Int age= 5;


Example 3
int i = 4;
object obj = i;
int b = (int) obj;


Stack
inta i =4;
Stack
obj (ref)
Heap
obj = 4;
Stack
int b= 4;


What are generation on Garbage Collection?
There are 3 generation of garbage collection
generation 0: Short live object are stored and release most frequently.
generation 1: Long lived object pushed into this, release more frequently.
generation 2:Longer lived object pushed into this, release least frequently.


  1. Generation 0 identifies a newly created object that has never been marked for collection
  2. Generation 1 identifies an object that has survived a GC (marked for collection but not removed because there was sufficient heap space)
  3. Generation 2 identifies an object that has survived more than one sweep of the GC.


What is anonymous method in C#?
Anonymous method is a technique by using which we can pass code block inside a delegate. Anonymous methods are the methods without a name, just the body.
You need not specify the return type in an anonymous method. it is inferred from the return statement inside the method body.


delegate void NumberChanger(int n);
NumberChanger nc = delegate(int x)
{
  Console.WriteLine("Anonymous Method: {0}", x);
};


What is generic class in Collection ?
What is difference between framework 4.5 and 4.6?
What is Design Pattern?
What is Singleton Pattern?
Which SDLC approach you are using in your organization?


How to write Query for left Join in Linq?
What is lambda expression?
What are differences between Azure and Ms sql server?


Multiple catch block in exception?
We can  have multiple catch for a try block.
But we can not have (Exception ex1) on top because it catches all exception  as super type. Never when one catch block is executed, controls skip all other catch blocks and goes to finally block.
Wrong: -
try
           {
              --------
           }
           catch (Exception ex2)
           {
              --------
           }
           catch (DivideByZeroException ex1)
           {
               ----------
           }
           finally
           {
               -------------
           }


Error 1 A previous catch clause already catches all exceptions of this or of a super type ('System.Exception') E:\C# Examples\Multiple Catch Block Exception\Multiple Catch Block Exception\Program.cs 26 20 Multiple Catch Block Exception
Right :-
try
           {
              --------
           }
catch (DivideByZeroException ex1)
           {
            }
           catch (Exception ex2)
           {
           }
 finally
           {}
What are the differences between System.String and System.Text.StringBuilder classes?
String is immutable.
Immutable means once we create string object we cannot modify. Any operation like insert, replace or append happened to change string simply it will discard the old value and it will create new instance in memory to hold the new value.
Example: string str = "dev"; onestr = "endra"; str = "help";


StringBuilder is mutable it means once we create stringbuilder object we can perform any operation like insert, replace or append without creating new instance for every time. Example: StringBuilder sb = new StringBuilder(""); sb.Append("hi"); sb.Append("test "); string str = sb.ToString();
          
        
What is the difference between Finalize() and Dispose() methods?
Finalize is used internally by garbage collector Used to free unmanaged resources like files, database connections, COM etc. held by an object before that object is destroyed. but Dispose() is used by used externally


IEnumerable VS IQueryable?



Both are the collection interface that is use to iterate the collection in forward manners. These are the points that we need to keep in our mind while implementing IEnumerable or IQueryable.


Differences:


  • IEnumerable is exists in System.Collection, where as IQueryable exists in System.Linq.
  • IEnumerable is best over in-memory collection like List, Array etc, where as IQueryable is best over out-memory collections like database and services.
  • IEnumerable is best for Linq to XML, IQueryable best for Linq to SQL.
  • IEnumerable has no methods like create, add and more it's read only can't customized, whereas IQ
  • ueryable has CreateQuery and Execute methods can customized.
  • IEnumerable doesn't support lazy loading, IQueryable support lazy loading.
  • IEnumerable execute in-memory after formulating query structure, where as IQueryable formulate complete query and executes on remote server.


Example:-
IEnumerable<>
DBContext dc = new DBContext ();
IEnumerable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("R"));
list = list.Take<Employee>(10);
SELECT [t0].[EmpID], [t0].[EmpName] FROM [Employee] AS [t0] WHERE [t0].[EmpName] LIKE @p0
Now filter 10 records in client memory.


IQueryable<>
DBContext dc = new DBContext ();
IQueryable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("R"));
list = list.Take<Employee>(10);
SELECT TOP 10 [t0].[EmpID], [t0].[EmpName] FROM [Employee] AS [t0] WHERE [t0].[EmpName] LIKE @p0