Few commonly asked .NET Interview Questions

.NET Framework:

1) Architecture of .NET Framework?
2) Architecture of CLR?
3) What is CLR?
4) What is CTS?
5) What is CLS?
6) MSIL?
7) What is JIT Compiler?
8) ASP.net Page life cycle?
9) Difference between web.config and global.asax? Continue reading

Advertisements

== and Equals() Method in C#

When we create any object there are two parts to the object one is the content and the other is reference to that content.So for example if you create an object as shown in below code:-“.NET interview questions” is the content.“o” is the reference to that content.object o = “.NET Interview questions
When we create any object there are two parts to the object one is the content and the other is reference to that content.
Note:
When you are using string data type it always does content comparison. In other words you either use “.Equals()” or “==” it always do content comparison. 
The rule of thumb is that for nearly all reference types, use Equals when you want to test the equality rather than reference identity. The exception is for strings, comparing strings with == does make things much simpler and more readable but you need to remember that both sides of the operator must be expressions of type string for the comparison to work properly.
So for example if you create an object as shown in below code:-

  1. “.NET interview questions” is the content.
  2. “o” is the reference to that content.

 “==” compares if the object references are same while “.Equals()” compares if the contents are same.

So if you run the below code both “==” and “.Equals()” returns true because content as well as references are same.

object o = ".NET Interview questions";
object o1 = o;
Console.WriteLine(o == o1);
Console.WriteLine(o.Equals(o1));
Console.ReadLine();
-----------

True
True

Now consider the below code where we have same content but they point towards different instances. So if you run the below code both “==”   will return false and “.Equals()”  will return true.

object o = ".NET Interview questions";
object o1 = new string(".NET Interview questions".ToCharArray());
Console.WriteLine(o == o1);
Console.WriteLine(o.Equals(o1));
Console.ReadLine();

Now let's see one more example that shows that the Equals() method is an extension method of the string class when you assign a null value to the string variable and using that variable the Equals() method then gets an exception of a null reference so you must be sure that your variable doesn’t have null values when calling the Equals() method. When your one variable contains a null value then you should use that variable as an argument for the Equals() method.

myName.Equals(name);  // Throws NullReference exception if myName is Null.
Null Reference Exception
 
The rule of thumb is that for nearly all reference types, use Equals when you want to test the equality rather than reference identity. The exception is for strings; comparing strings with == does make things much simpler and more readable but you need to remember that both sides of the operator must be expressions of type string for the comparison to work properly.

 

 

 

 

IDisposable and GC

My assumption is that if I implement IDispose on an object, I can explicitly ‘destruct’ it as opposed to waiting for the garbage collector to do it. Is this correct?

The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as DB connections, or open files and streams.  Dispose is not about memory management, it’s about unmanaged resource management Continue reading

MVC app vs. Single Page App

We build a single page app instead of a standard MVC app to avoid re-rendering pages and performance issues. Also SPAs can provide a rich, desktop-like environment.  But what to choose ..?

SPA needs heavy jQuery Ajax to build frontend and mixed frontend tools like Angular JS, duplicate js models, Knockout JS to do the binding and etc… SPA looks pretty, fancy and responsive. but, it’s hard to unit test since its complicated and mixed technologies all over the place. I would suggest to use Regular MVC structure while build medium to large app, utilize area to grouping the feature of your app. you can implement good security measurement, apply strong validations to your app. your app will close to HTML easy for maintenance, light jQuery that enhances the UI is already good when you want to focus on the functions and features instead of fancy UI. Continue reading

Difference between an Abstract class and an Interface.

There are technical differences between Abstract Classes and Interfaces, that being an Abstract Class can contain implementation of methods, fields, constructors, etc, while an Interface only contains method and property prototypes.

Two quick thoughts on differences between interfaces and abstract classes:

  1. Abstract classes desired if future expansion is likely, as an abstract class can be expanded, but an interface would have to be enhanced by addition of another interface, I2.

2. A class can inherit multiple interfaces but only one abstract class.

An Interface defines what something can do (how it behaves), and an Abstract Class defines what something is. Continue reading

Detention Time Calculator.

Johnny has been a bad boy. He has committed several offenses and played a lot of pranks at school. He has been caught and now Mr. Wilson, the school head master has to give him detention. The period of detention depends on several factors as per rules of the school board and poor Mr. Wilson needs some help. Continue reading