.Net Interview Question Answers


·        IDisposable vs finalizer (destructor)
o   IDisposable is used to deterministically clean up objects e.g. close files, database connections, etc. Usually used to release managed as well as unmanaged resources. We have to implement Dispose() method of IDisposable interface. We can call Dispose() explicitly by user code
o   A finalizer is part of garbage collection. It is not deterministic as GC mainly happens when we need more memory space. Usually used to release unmanaged resources. It is called internally, it is called by Garbage Collector only. We need to implement destructor of object which internally converted to Finalize()
·        Trace vs Debug
o   Debug and Trace classes (of namespace System.Diagnostics) are used to provide information about the program execution. Using WriteLine(), WriteLineIf(), Assert() etc. methods
o   If we set Solution Configuration to Debug then methods of Trace are ignored and if we set Solution Configuration to Release then methods of Debug class are ignored
·        Throw vs throw ex
o   throw ex, resets the stack trace
o   throw, doesn't - the original stack trace would be preserved.
·        typeof(obj) vs obj.GetType()
o   typeof(type) is applied to a name of type or a generic type known at compile time
o   obj.GetType() is called on an object at runtime.
o   Both returns object of type System.Type containing metadata of object
o   string s = "hello"; Type t1 = typeof(String); Type t2 = s.GetType(); t1 == t2 => true
o   object o = "hi"; Type t1 = typeof(o); Type t2 = o.GetType(); t1 == t2 => false (object != string)
·        == vs obj.equals(obj)
o   The == Operator compares the reference identity
o   Equals() method compares only contents
o   New StringBuilder(‘vipul’) == New StringBuilder(“vipul”); => False
o   new StringBuilder("vipul").Equals(new StringBuilder("vipul")); => True
·        Ref vs out parameter
o   In ref, parameter must be initialized before passing to a function, not required in out
o   In out, parameter can’t read before initialized, not necessary in ref
o   In out, function must set value to out parameter, not required in ref
o   Out is unidirectional, we cannot pass data to called function using out parameter because before reading out parameter needs to be initialized, ref is bidirectional
·        DataReader , DataSet, DataAdapter and DataTable
o   DataReader is used to read the data from database and it is a read and forward only connection oriented architecture during fetch the data from database. It will fetch the data very fast when compared with dataset. Generally we use ExecuteReader object to bind data
o   DataSet is a disconnected architecture and it is a collection of DataTables and relations between tables. It holds multiple tables with data. You can select data form tables, create views based on table and ask child rows over relations.
o   DataAdapter acts as a Bridge between DataSet and database. This object is used to read the data from database and bind that data to dataset. It is a disconnected architecture.
o   DataTable represents a single table of database. There is no much difference between DataSet and DataTable, DataSet is simply the collection of DataTables.
·        Const vs ReadOnly
o   We must declare the value at the time of a definition for a const. ReadOnly values can be computed dynamically but need to be assigned before the constructor exits after that it is frozen.
o   Const's are implicitly static. You use a ClassName.ConstantName to access them.
o    
·        Thread vs Process
o   An application consists of one or more processes. A process is an executing program. One or more threads run in the context of the process. A thread is the basic unit to which the operating system allocates processor time. A thread can execute any part of the process code, including parts currently being executed by another thread.
·        EXE vs DLL
o   EXE: It's an executable file with an entry point (main method) that will run on execution. It always runs in its own address space so when system launches new executable, a new process is created. The entry thread is called in context of main thread of that process.
o   DLL: It is Microsoft's implementation of a shared library. It always needs a host exe to run. System loads a DLL into the context of an existing process. The purpose of a DLL is to have a collection of methods/classes which can be re-used from some other application
·        Short-circuited operators
o   Short circuited logical operators in C# are && and ||, they are efficient and fast versions of logical operators & and |.
o   In case of & or |, all the expressions are executed. If (false & fun()); fun() will be called.
o   In case of &&, if first expression is failed then remaining expressions are not executed. If (false && fun()); fun() will not be called.
·        Reflection
o   Reflection (System.Reflection) provides objects that describe assemblies, modules and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, reflection enables you to access them
·        Strong-Named Assemblies
o   Strong-naming an assembly creates a unique identity for the assembly (using the private key that corresponds to the public key distributed with the assembly) and can prevent assembly conflicts.
o   It contains simple text name of the assembly, the version number, optional culture information, a digital signature, and the public key that corresponds to the private key used for signing.
·        Caching in Asp.Net
o   A cache stores the output generated by a page in the memory and this saved output/ cache will serve to users in future.
o   Page Caching will store complete page in memory using OutputCache directive. <%@ OutputCache Duration = 5 VaryByParam = "ID" %> directive is used for page cache where duration defines the number of seconds the cache will be stored, after that it will be cleared. varyByParam defines the QueryString parameter to vary the cache. We can have multiple parameters separated by semicolon(;) in varyByParam
o   Fragment Caching will store part of page in memory using User Control. Implementation is same as page cache
o   Data Caching will store data objects in memory using Cache class. We can use cache like Cache[“name”]=”XYZ”; or Cache.Insert(“Name”,”XYZ”, )
·        Cookies
o   Namespace: Systen.Web.HttpCookie
o   It is a small piece of information stored on client machine in plain text format. This file is located at "C:\Document and Settings\UserName\Cookie" path. It is used to store user information like Username, Password etc.
o   Persist CookieWithout expiry time
o   Non-Persist CookieWith expiry time
o   We can use either HttpCookies object or Response Object to create cookies
§  HttpCookie userInfo = new HttpCookie("userInfo");
        userInfo["UserName"] = "XYZ";
        userInfo.Expires = DateTime.Now.AddDays(1);
        Response.Cookies.Add(userInfo);
·        Response.Cookies["userName"].Value = "XYZ";
o   Similarly, we can use either one of both objects to retrieve cookies data
§  String name = Request.Cookies["userName"].Value;
§  HttpCookie reqCookies = Request.Cookies["userInfo"];
String name = reqCookies["UserName"].ToString();
·        Function vs Stored Procedure
o   Functions are computed values and cannot perform permanent environmental changes to SQL Server (i.e. no INSERT or UPDATE statements allowed)
o   Function must return a value, not necessary in case of SP
o   Function must have at least one parameter, not necessary in SP
o   Function can have only input parameter, SP can have both input/ output parameter
o   Functions can be called from SP, reverse is not possible
o   Functions can be used in SELECT/ WHERE/ HAVING statements, not possible for SP
o   Try Catch block are used to handle exceptions in SP, function doesn’t support Try Catch
·        Loading Sequence of Master Page, Contents Page and User Control
o   Page PreInit
o   User Control Init
o   Master Page Init
o   Page Init
o   Page Load
o   Master Page Load
o   User Control Load
·        Why we use sealed class for singleton design pattern
o   Though we make default constructor as private, if we don’t use sealed class then we will be able to inherit the singleton class in child/nested class.
o   So once we inherit singleton class in child/nested class, the object of the singleton class is gets created.
·        Delegates
o   A delegate (function pointer in C/C++) is a reference type that invokes single/multiple methods through the delegate instance. It holds a reference of the methods. Delegate types are sealed and immutable type.
o   It is used to call multiple methods on a single event, define callback (asynchronous) methods, invoke method at runtime
o   Single Delegate used to invoke a single method
o   Multicast Delegate used to invoke the multiple methods. The delegate instance can add new method on existing delegate instance using + operator and  operator can be used to remove a method from a delegate instance. All methods will invoke in sequence as they are assigned.
o   Generic Delegate don't require to define the delegate instance in order to invoke the methods. Following are types of Generic Delegate;
§  Func delegate defines a method that can be called on arguments and returns a result. It can have 0 to 16 input parameters of different types
§  Action delegate defines a method that can be called on arguments but does not return a result. It can have 1 to 16 input parameters of different types
§  Predicate delegate defines a method that can be called on arguments and always returns Boolean type result