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

What is anonymous type in C#?

This is a new feature in C# 3.0. This enable use to create a type/class on-the-fly at compile time.

For example

var emp = new { Name = "Sheo", Gender = "Male", Active = true };

In this case a new type will be created on the fly that will have Name, Gender, and Active as public property and its backing field like _Name, _Gender, _Active.

This is especially useful when we want to receive data from other object or iterate through a collection and set values and do not want to create a class just to hold the data. Note that anonymous types are just a placeholder, we can't customize its behavior or add methods inside it.
What is extension method in C#?

This is a new feature in C# 3.0. This method allows us to add a new static method to the existing classes.

For example, if we want to validate an Email address using the static method of the string class (built-in) class, we can add an extension method like this.

public static bool IsValidEmail(this string input)

{
Regex regEx = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
return regEx.IsMatch(input);
}


Notice the parameter passed to above method. Here "this" indicates the string class. Now, we can use above extension method like this.

string myEmail = "me@me.com";

bool IsValidEmailAddress = myEmail.IsValidEmail();


In this case as the format of the email id in myEmail variable is correct so IsValidEmailAddress will be true.
Constructor and Destructor

Constructor:
-----------------

1. The Constructor is the first method that is run when an instance of a type is created. In visual basic a constructor is always Sub new ().

2. Constructor are use to initialize class and structure data before use. Constructor never returns a value and can be overridden to provide custom initialization functionality.

3. The constructor provides a way to set default values for data or perform other necessary functions before the object available for use.

Destructor:
---------------
Destructors are called just before an object is destroyed and can be used to run clean-up code. You can’t control when a destructor is called.
Differrence between Array.CopyTo() and Array.Clone()

Both CopyTo() and Clone() make shallow copy (data is copied) and used for Single Dimension arrays.

Clone() method makes a clone of the original array. It returns an exact length array.

CopyTo() copies the elements from the original array to the destination array starting at the specified destination array index. Note that, this adds elements to an already existing array.

In VB.NET, variables has to be declared earlier, before using Clone or CopyTo methods to store the values. The difference arise on mentioning the size of the array.

While declaring the varaiable that is used for CopyTo method, it should have the size equal to or more than that of the original array.

While declaring the variable that is used for Clone method we need not mention the array size. Eventhough the array size is small, it won't show any error, rather the array size gets altered automatically on cloning. The cloned array size get created equal to the size of the source array.

If value type is used for CopyTo/Clone any change in the values made on them will not get reflected on the original whereas on reference type the change gets reflected.

Below is the code which would throw some light on your understanding.
what is Static?

Static:
1)A Static memberdata or a memberfunction can be called without the help of an object.
2)Static members can be called with the help of Classname.

Syntax: classname.staticmember
3)'this ' keyword cannot be used inside a static memberfunction
4)Nonstatic members cannot be used inside a static memberfunction
What’s the advantage of using System.Text.StringBuilder over System.String?

StringBuilder is more efficient in cases where there is a large amount of string manipulation. Strings are immutable, so each time a string is changed, a new instance in memory is created.
When do you absolutely have to declare a class as abstract?

1. When the class itself is inherited from an abstract class, but not all base abstract methods have been overridden.
2. When at least one of the methods in the class is abstract.
What’s a delegate?

A delegate object encapsulates a reference to a method.
What’s a multicast delegate?

A delegate that has multiple handlers assigned to it. Each assigned handler (method) is called.
What does assert() method do?

In debug compilation, assert takes in a Boolean condition as a parameter, and shows the error dialog if the condition is false. The program proceeds without any interruption if the condition is true.
What is a satellite assembly?

When you write a multilingual or multi-cultural application in .NET, and want to distribute the core application separately from the localized modules, the localized assemblies that modify the core application are called satellite assemblies.
can we declare a block as static in c#?

NO,because c# doesnot support static block,but it supports static method
How and why to use Using statement in C#?

Using statement is used to work with an object in C# that inherits IDisposable interface.

IDisposable interface has one public method called Dispose that is used to dispose off the object. When we use Using statement, we don't need to explicitly dispose the object in the code, the using statement takes care of it. For eg.

Using (SqlConnection conn = new SqlConnection())

{

}

When we use above block, internally the code is generated like this
SqlConnection conn = new SqlConnection()
try

{

}
finally
{
// calls the dispose method of the conn object
}


Using statement make the code more readable and compact.

For more details read http://www.codeproject.com/KB/cs/tinguusingstatement.aspx
What is the name of C# compiler?

The name of C# compiler is CSC.

eg. to create the executable of the .cs file you can use

csc File.cs 

This will create File.exe file.

For more details visit: http://msdn.microsoft.com/en-us/library/78f4aasd.aspx
What is object pooling?

Object Pooling is something that tries to keep a pool of objects in memory to be re-used later and hence it will reduce the load of object creation to a great extent.

Object Pool is nothing but a container of objects that are ready for use. Whenever there is a request for a new object, the pool manager will take the request and it will be served by allocating an object from the pool.

This concept has been better explained in following article
http://www.c-sharpcorner.com/UploadFile/vmsanthosh.chn/109042007094154AM/1.aspx
How to create a strong name?

To create a strong name key use following code

C:\>sn -k s.snk


Strong name key is used to specify the strong name used to deploy any application in the GAC (Global Assembly Catch) in the system.
What is Global Assembly Cache?

Abbreviated as GAC, the Global Assembly Cache is a machine-wide store used to hold assemblies that are intended to be shared by several applications on the machine. Each computer where the common language runtime (CLR) is installed has a global assembly cache. The global assembly cache stores the assemblies specifically designated to be shared by several applications on the computer.

For more details visit http://www.webopedia.com/TERM/G/Global_Assembly_Cache.htm
What is Partial Class?

Partial class

Instead of defining an entire class, you can split the definition into multiple classes by using the partial keyword. When the application is complied, the C# complier will group all the partial classes together and treat them as a single class. There are a couple of good reasons to use partial classes. Programmers can work on different parts of a class without needing to share the same physical file. Also you can separate your application business logic from the designer-generated code.

C#

Public partial class Employee

{
public void DoWork()
{}
}

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

What is operators in C# and how many types of operators are there in c#?

C# has a large set of operators, which are symbols that specify which operations to perform in an expression.

Arithmetic
+ - * / %

Logical (boolean and bitwise)
& | ^ ! ~ && || true false

String concatenation
+

Increment, decrement
++ --

Shift
<< >>

Relational
== != < > <= >=

Assignment
= += -= *= /= %= &= |= ^= <<= >>=

Member access
.

Indexing
[]

Cast
()

Conditional
?:

Delegate concatenation and removal
+ -

Object creation
new

Type information
as is sizeof typeof

Overflow exception control
checked unchecked

Indirection and Address
* -> [] &

For more details visit http://msdn.microsoft.com/en-us/library/6a71f45d(VS.71).aspx
What is operator overloading in C#?

Operator overloading is a concept that enables us to redefine (do a special job) the existing operators so that they can be used on user defined types like classes and structs.

For more information visit: http://aspalliance.com/1227_Understanding_Operator_Overloading_in_C.all
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