C# Interview Questions and Answers (958) - Page 27

What is the difference between IEnumerable and IList?

IEnumerable

IEnumerable<T> represents a series of items that you can iterate over (using foreach, for example)
IEnumerable doesn't have the Count method and you can't access the collection through an index (although if you are using LINQ you can get around this with extension methods).
IEnumerable is a general-purpose interface that is used by many classes, such as Array, List and String in order to let someone iterate over a collection. Basically, it's what drives the foreach statement.
As a rule of thumb you should always return the type that's highest in the object hierarchy that works for the consumers of this method. In your case IEnumerable<T>.

IList

IList<T> is a collection that you can add to or remove from.
List has count method and accessed using index.
IList is typically how you expose variables of type List to end users. This interface permits random access to the underlying collection.
If the consumers of the class need to add elements, access elements by index, remove elements you are better off using an IList<T>.
Shallow Copy VS Deep Copy

Shallow copy

Copies the structure of the object

The easiest way to think of it is a shallow copy allows you to replicate the array once... it then ignores the relationship it has with the original array.

OriginalArray = ShallowArray.Clone();
The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object

For example, consider an object called X that references objects A and B. Object B, in turn, references object C. A shallow copy of X creates new object X2 that also references objects A and B.

Deep copy

Copies structure as well as data.

The deep copy will be persistent and any changes you make to the original array will be reflected in both copies.

OriginalArray = DeepArray;

In contrast, a deep copy of X creates a new object X2 that references the new objects A2 and B2, which are copies of A and B. B2, in turn, references the new object C2, which is a copy of C. The example illustrates the difference between a shallow and a deep copy operation.
Can we Inherit the class defined with private constructor?

No, we can not inherit the class defined with private constructor.
 public class A:B

{
}
public class B
{
private B()
{
}
}

Here if we will compile this code error will come:
Namespace.B.B() is inaccessible due to its protection level
By which method we can get the version of installed .Net Framework?

NOTE: This is objective type question, Please click question title for correct answer.
What will be out put of following code: static void doIncrement(ref int param) { param++; } static void Main() { int arg; // not initialized doIncrement(ref arg); Console.WriteLine(arg); }

It will give Compile time error:Use of unassigned local variable 'arg'
as C# enforces the rule that we must assign a value to a variable before we can read it. This rule also applies to method arguments, we cannot pass an uninitialized value as an argument to a method even if an argument is defined as a ref argument.
Can we declare a default constructor (a constructor with no parameters) for a structure?

We can’t declare a default constructor (a constructor with no parameters) for a structure. The following example would compile if Time were a class, but because Time is a structure, it does not:
struct Time

{
public Time() { ... } // compile-time error
...
}

The reason we can’t declare our own default constructor for a structure is that the compiler always generates one. In a class, the compiler generates the default constructor only if we don’t write a constructor ourself. The compiler-generated default constructor for a structure always sets the fields to 0, false, or null—just as for a class.
what is managed and unmanaged code ?

Answer to this question is --- Code that executes under CLR environment is called Managed code for example code written in c# needs CLR Environment to execute it.
Unmanaged code is a code that can execute without any CLR environment Just like code written in c,c++ languge.
what is the purpose of using int.Parse and Convert.ToInt32 method ?

AS we know here in C sharp we have number of ways to using that we can change our string data type into integer data type, two of them are int.Parse and Convert.ToInt32.
Both are basic method and used to convert string data type into integer data type. But question arises here why we go for other method ? if we have one method like int.Parse that works same as that other method such as Convert.ToInt32 and other else works.
Reason are ,suppose if our string containing null to which we want to convert into integer while using int.Parse method, in this case we will get exception error at run time.
For the same situation where we want to convert our null string into integer using Convert.ToInt32 method, it will compile successfully without generating any exception error.
In other words , if we are sure that our numeric value will remains numeric after conversion than we should use int.Parse method.
But if we are not sure our numeric value after conversion may or may not be "Null" than we should use Convert.ToInt32 Method,it works for Both numeric and Null value and never gives exception error.
What is abstract method or abstract method is found in Interface.

Abstract methods are declared in Abstract Class.
All abstract method have to compulsory Implemented in the subclass(Abstract derived class). But other methods in Abstract class, except abstract method, not compulsory to Implement its derived class.

In Interface all methods are abstract method(So no need to declare abstract method).
what is abstract class?

An abstract class contains abstract and non abstract method.we can declare it by using keyword abstract.
The abstract keyword allows us to create any class and its member , that may be incomplete and must be implemented in a derived.
Using abstract class multiple derived classes can share definition of base class.

Lets take an example -
Suppose we are building a house, where we want All room must be same area.. what we will do ?
we will make a method ,Suppose it is RoomArea() and implements its body ..here we have forced to architect that All room must be equal in Area therefore we have made method and its definition.
Lets take other situation , Now we move to door's color.. This time we left it over architect,he is free to Apply any color of his choice on door.we are not interested in color.
what we have done here ,we have bounded to architect that method should be our choice, so i am giving you its body but i am not interested in door's color so you can implement it in your way.
abstract class House
{
public void RoomArea()
{ // body ...All room must be equal in area
// it is normal method
}
abstract public void DoorColor(); // Abstract method
}


Few important points about abstract class -
1. We can not use abstract class without inheriting it.
2. Only abstract members are possible in abstract class.
3. Abstract class can have constructor.
4. We Can not create direct object of abstract class.
5. Any Class derived fro an abstract class must implement all the abstract members of the class by using the override keyword unless the derived class itself abstract.
What is the difference between static and const data member ?

Static
1. Static member is a reference type.
2.Variable set at run time.
3.Static member can be accessed without creating instance of the class or can be
changed from many location.

Const
1.Cost member is a value type.
2.Variable set at compile time.
3.It can't be changed in the application creating instance of the class or can be anywhere else in the code.
VAR - DYNAMIC DIFFERENCE C# 4.0?

1. The Var(Implicit typed local variable) keyword is used to define local variables.In case of Var , the underlying data type is determined at compile time itself based on the initial assignment.Once the initial assignment has been made with Var type , then it will become strongly typed.If you try to store any incompatible value with the Var type it will result in compile time error.

Example:

Var strNameList=new List<string>(); By using this statement we can store list of names in the string format.
strNameList.add("Senthil");
strNameList.add("Vignesh");

strNameList.add(45); // This statement will cause the compile time error.

But in Dynamic type, the underlying type is determined only at run time.Dynamic data type is not checked at compile time and also it is not strongly typed.We can assign any initial value for dynamic type and then it can be reassigned to any new value during its life time.

Example:
dynamic test="Senthil";
Console.Writeline(test.GetType()) // System.String

test=1222;
Console.Writeline(test.GetType()) // System.Int32

test=new List<string>();
Console.Writeline(test.GetType()) //System.Collections.Generic.List'1[System.String]

It doesn't provide IntelliSense support also.It doesn't give better support when we give work with linq also.Because it doesn't support lambda expressions ,extension methods and anonymous methods.

To know more about the Var and Dynamic, please have a look at my blog:-
http://senthilvijayalakshmi.blogspot.in/2013/03/difference-between-var-and-dynamic.html
How Dynamic Works in C# 4.0?

How Dynamic works in C#

DLR (Dynamic language runtime) is set of services which add dynamic programming capability to CLR. DLR makes dynamic languages like LISP, Javascript, PHP,Ruby to run on .NET framework.

To know more about DLR, please visit the following link:-
http://www.codeproject.com/Articles/42997/NET-4-0-FAQ-Part-1-The-DLR

We are going to see how the Dynamics work in .Net now. For example consider the following statement.

dynamic d=GetSomeData(15); When we execute this statement,Let's see what will happen behind the scenes.
When the above statement executes, the DLR will pass the expression trees to the target object.If the dynamic data type is pointing in memory to a COM object, then the expression tree will be sent to the COM interface named IDispatch. This interface is COM's way of incorporating its own set of dynamic services.
If the dynamic data is not pointing to a COM object, then the expression tree may be passed to the object implementing the IDynamicObject interface.This interface is used to map the expression tree to Ruby specifics in the dynamic languages like Iron Ruby.
If suppose the dynamic data is not pointing to COM object and does not implement IDynamicObject, then the object will be considered as .Net object. In this case the expression tree is sent to the C# run time binder for processing.
After the expression tree has been processed by the given binder, the dynamic data will be resolved to the real in-memory data type after which the correct method is called with any necessary parameters.

To know more about the expression tree basics , please read the following article.
http://www.codeproject.com/Articles/235860/Expression-Tree-Basics
Difference between Generic List and Array List in C#?

1. ArrayList and Generic List are used to store any type of objects.Consider that you are intended to store only the integer values.In this case when we use ArrayList , we can store any type of objects(i.e., Integer,String or some custom objects).

Example:
ArrayList objarList=new ArrayList();
objarList.add(123);
objarList.add("senthil");// will compile..but will throw run time error.

It won't throw any compile time error.It will throw only run time error when we iterate values in the array list by using for each.

But when we use the Generic List ,we can restrict in the compile time itself.If you are intended to store only integer values,then it will allow to store only integer values.If you try to store any other value other than integer, then it will throw compile time error.

List<int> objList=new List<int>();
objList.add(123);
objList.add("senthil") // Will result in compile time error.

2.Whenever you are adding the objects in the ArrayList boxing will happen at that time.Whenever you are accessing the objects from the ArrayList unboxing will happen at that time.So it will hurt the performance. But in case of Generic List there will not be any Boxing or UnBoxing problem and also it will provide the better type safety.
CAN WE PAUSE WINDOWS TIMER CONTROL?

NO,
The timer control is used to perform iterative tasks at specified time intervals. It has only Start and Stop properties. So we cannot PAUSE it.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories