Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Search Your Question

Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, May 5, 2008

How is method overriding different from method overloading?

When overriding a method, we change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same name within the class.

Can you declare an override method to be static if the original method is not static?

No. The signature of the virtual method must remain the same. (Note: Only the keyword virtual is changed to keyword override)

What are jagged arrays?

A jagged array is an array whose elements are arrays.
The elements of jagged array can be of different dimensions and sizes. A jagged array is sometimes called as “array-of-arrays”. It is called jagged because each of its rows is of different size so the final or graphical representation is not a square.

For e.g. :

int [] [] myJaggedArray = new int [3][];

Declaration of inner arrays:
myJaggedArray[0] = new int[5] ; // First inner array will be of length 5.
myJaggedArray[1] = new int[4] ; // Second inner array will be of length 4.
myJaggedArray[2] = new int[3] ; // Third inner array will be of length 3.

Now to access third element of second row we write:

int value = myJaggedArray[1][2];

While declaring the array the second dimension is not supplied because this you will declare later on in the code.

Since Jagged array are created out of single dimensional arrays don’t confuse it with multi-dimensional arrays because unlike them jagged arrays are not rectangular arrays.

What is a delegate, why should you use it and how do you call it ?

Delegate lets some other code call your function without needing to know where your function is actually located.
All events in .NET actually use delegates in the background to wireup events. Events are really just a modified form of a delegate.
We can say that a delegate is a class that has a signature and holds references to methods.

A sample delegate definition can be .

Why string are called Immutable data Type ?

The contents of the String class cannot be changed. any method that you might expect to modify the contents actually returns a new instance of the string. The old instance remains in the memory untill its cleaned by the GC.

The memory representation of string is an Array of Characters, So on re-assigning the new array of Char is formed & the start address is changed . Thus keeping the Old string in Memory for Garbage Collector to be disposed.

What do you understand by a Reference type and value type ?

Reference Type:

Reference types are allocated on the managed CLR heap, just like object types.
A data type that is stored as a reference to the value's location.

Value Type:

Value types are allocated on the stack just like primitive types in VBScript, VB6 and C/C++.
Value types are not instantiated using new go out of scope when the function they are defined within returns.
Value types in the CLR are defined as types that derive from system.valueType.

Define Boxing and unboxing ?

Boxing:The conversion of a value type instance to an object.
Un-Boxing:The conversion of an object instance to a value type.

What's the advantage of using System.Text.StringBuilder over System.String?

StringBuilder is more efficient in the cases, where a lot of manipulation is done to the text. Strings are immutable, so each time it's being operated on, a new instance is created

Can you store multiple data types in System.Array?

No , Its not possible

What's an abstract class?

A class that cannot be instantiated. An abstract class is a class that must be inherited and have the methods overridden. An abstract class is essentially a blueprint for a class without any implementation.

What are class access modifiers ? How many types ?

Access modifiers are keywords used to specify the declared accessibility of a member or a type.
Public - Access is not restricted.
Protected - Access is limited to the containing class or types derived from the containing class. Internal - Access is limited to the current assembly.·
Protected inertnal - Access is limited to the current assembly or types derived
Private - Access is limited to the containing type.

What is the difference between structures and enumeration?

Unlike classes, structs are value types and do not require heap allocation. A variable of a struct type directly contains the data of the struct, whereas a variable of a class type contains a reference to the data. They are derived from System.ValueType class.

Enum->An enum type is a distinct type that declares a set of named constants.They are strongly typed constants. They are unique types that allow to declare symbolic names to integral values. Enums are value types, which means they contain their own value, can't inherit or be inherited from and assignment copies the value of one enum to another.
public enum Levels
{
One, Two, Three
}

Is it possible to use multipe inheritance in C# / VB.Net?

Multiple Inheritance is an ability to inherit from more than one base class i.e. ability of a class to have more than one superclass, by inheriting from different sources and thus combine separately-defined behaviors in a single class.

There are two types of multiple inheritance: multiple type/interface inheritance and multiple implementation inheritance.

C# & VB.NET supports only multiple type/interface inheritance, i.e.you can derive an class/interface from multiple interfaces.

There is no support for multiple implementation inheritance in .NET. That means a class can only derived from one class.

What is the Difference between Int and Int32 in .Net?

There is no difference in these two.

When do you use virutal keyword?

We use virtual when we need to override a method of the base class from the sub class, then we give the virtual keyword in the base class method. This makes the method in the base class to be overridable.

What are sealed classes

Classes marked sealed cannot be inherited further. The keyword “sealed” will prevent the class from being inherited.

What’s a multicast delegate?

A multicast delegate is a delegate that has multiple handlers assigned to it. Each assigned handler (method) is called.

What’s the difference between struct and class in C#?

1 Structs cannot be inherited.
2 Structs are passed by value, not by reference.
3 Struct is stored on the stack, not the heap.

What are valid signatures for the Main function in C#?

1) public static void Main()
2) public static int Main()
3) public static void Main( string[] args )
4) public static int Main(string[] args )

Does Main() always have to be public in C#?

No not necessarily

More discussions related to this at http://www.dotnetinterviews.co.nr/