Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Saturday, April 8, 2017

C# Interview Questions and Answers 5

1) Why to use “using” in C#?

-The reason for the "using" statement is to ensure that the object is disposed as soon as it goes out of scope, and it doesn't require explicit code to ensure that this happens.

using (SqlConnection cnn = new SqlConnection())
  cnn .open();

It is like ---You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block;
    SqlConnection cnn = new SqlConnection();
       cnn .open();
       if (cnn != null)
As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. 
Within the using block, the object is read-only and cannot be modified or reassigned.
The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object.

2) What is the difference between “dispose” and “finalize” variables in C#?
finalize - The finalize() method is called when your object is garbage collected and you have no guarantee when this will happen (you can force it, but it will hurt performance). so, Finalize () is called by Garbage Collector implicitly to free unmanaged resources. The garbage collector calls this method at some point after there are no longer valid references to the object.

Dispose - There are some resources like windows handles, database connections which cannot be collected by the garbage collector. Therefore the programmer needs to call Dispose() method of IDisposable interface.
Dispose () of IDisposable interface is called by the programmer to explicitly release resources when they are no longer being used. Dispose () can be called even if other references to the object are alive.
The standard practice is to implement IDisposable and Dispose so that you can use your object in a using statment. Such as using(var foo = new MyObject()) { }.

3) What is Managed or Unmanaged Code?
Ans - 
Managed code is not compiled to machine code but to an intermediate language which is interpreted and executed by some service on a machine and is therefore operating within a secure framework which handles dangerous things like memory and threads for you. In modern usage this frequently means .NET but does not have to.
The resource, which is with in your application domain is, managed code. The resources that are within domain are faster.
The code, which is developed in .NET framework, is known as managed code. This code is directly executed by CLR with help of managed code execution. Any language that is written in .NET Framework is managed code.
Managed code uses CLR which in turns looks after your applications by managing memory, handling security, allowing cross - language debugging, and so on.

Unmanaged code is compiled to machine code and therefore executed by the OS directly. It therefore has the ability to do damaging/powerful things Managed code does not. This is how everything used to work, so typically it's associated with old stuff like .dlls
The code, which is developed outside .NET, Framework is known as unmanaged code.
Applications that do not run under the control of the CLR are said to be unmanaged, and certain languages such as C++ can be used to write such applications, which, for example, access low - level functions of the operating system. Background compatibility with code of VB, ASP and COM are examples of unmanaged code.
Unmanaged code can be unmanaged source code and unmanaged compile code.
Unmanaged code is executed with help of wrapper classes.

4) What is sealed class in c#?
Ans - Sealed classes are used to restrict the inheritance feature of object oriented programming. Once a class is defined as a sealed class, the class cannot be inherited.

In C#, the sealed modifier is used to define a class as sealed. In Visual Basic .NET the NotInheritable keyword serves the purpose of sealed. If a class is derived from a sealed class then the compiler throws an error.

5)List down the commonly used types of exceptions in .Net?
Ans -
ArgumentNullException ,
DivideByZeroException ,
OverflowException ,
 IndexOutOfRangeException ,
InvalidCastException ,
InvalidOperationException , 
IOEndOfStreamException , 
NullReferenceException , 
OutOfMemoryException ,
 StackOverflowException etc.

      No comments :

      Post a Comment