Thursday, March 4, 2010

.NET FAQs -2

1. What is arraylist?

An ArrayList is an array that can dynamically grow and shrink.The ArrayList is one of the important classes in the System.Collection namespace. ArrayList is a dynamical array; it will increase the size of the storage location as required. It stores the value as object.

ArrayList oArrayList = new ArrayList();
oArrayList.Add("Mark");
oArrayList.Add("John");

2. WCF Questions

3. WPF Questions

4. Difference between Overriding and Overloading.

Overriding - same method names with same arguments and same return types associated in a class and its subclass.

Overloading - same method name with different arguments, may or may not be same return type written in the same class itself.

Example for overriding
Clas A
{
Virtual void hi(int a)
{
}
}

Class B:A
{
public overrid void hi(int a)
{

}
}

Example for Over loading

Class A
{
class a()

{

}
class a(int a)
{
}
}

5. What is Virtual in C#?

The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it:

public virtual double Area()
{
return x * y;
}


using System;
class TestClass
{
public class Dimensions
{
public const double PI = Math.PI;
protected double x, y;
public Dimensions()
{
}
public Dimensions(double x, double y)
{
this.x = x;
this.y = y;
}

public virtual double Area()
{
return x * y;
}
}

public class Circle : Dimensions
{
public Circle(double r) : base(r, 0)
{
}

public override double Area()
{
return PI * x * x;
}
}

class Sphere : Dimensions
{
public Sphere(double r) : base(r, 0)
{
}

public override double Area()
{
return 4 * PI * x * x;
}
}

class Cylinder : Dimensions
{
public Cylinder(double r, double h) : base(r, h)
{
}

public override double Area()
{
return 2 * PI * x * x + 2 * PI * x * y;
}
}

static void Main()
{
double r = 3.0, h = 5.0;
Dimensions c = new Circle(r);
Dimensions s = new Sphere(r);
Dimensions l = new Cylinder(r, h);
// Display results:
Console.WriteLine("Area of Circle = {0:F2}", c.Area());
Console.WriteLine("Area of Sphere = {0:F2}", s.Area());
Console.WriteLine("Area of Cylinder = {0:F2}", l.Area());
}
}

6. Static class in C#

A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.

Use a static class to contain methods that are not associated with a particular object.

Suppose you have a class CompanyInfo that contains the following methods to get information about the company name and address.
C#

class CompanyInfo
{
public string GetCompanyName() { return "CompanyName"; }
public string GetCompanyAddress() { return "CompanyAddress"; }
//...
}

These methods do not need to be attached to a specific instance of the class. Therefore, instead of creating unnecessary instances of this class, you can declare it as a static class, like this:
C#

static class CompanyInfo
{
public static string GetCompanyName() { return "CompanyName"; }
public static string GetCompanyAddress() { return "CompanyAddress"; }
//...
}

7. Polymorphism in C#

When a message can be processed in different ways is called polymorphism. Polymorphism means many forms.

There are 2 types of Polymorphism namely
1.Compile time polymorphism (or) Overloading
2.Runtime polymorphism (or) Overriding

Compile Time Polymorphism :


Compile time polymorphism is method and operators overloading. It is also called early binding. Method with same name but with different arguments is called method overloading. In method overloading, method performs the different task at the different input parameters.


public class A1
{
public void hello()
{ Console.WriteLine(“Hello”); }

public void hello(string s)
{ Console.WriteLine(“Hello {0}”,s); }
}

Runtime Time Polymorphism :


Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding.Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass.

When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same prototype. Method overloading has nothing to do with inheritance or virtual methods.


public Class parent
{
virtual void hello()
{ Console.WriteLine(“Hello from Parent”); }
}

public Class child : parent
{
override void hello()
{ Console.WriteLine(“Hello from Child”); }
}

public static void main()
{
parent objParent = new child();
objParent.hello();
}

8. Generics in .NET

Generics allow you to realize type safety at compile time. They allow you to create a data structure without committing to a specific data type. When the data structure is used, however, the compiler makes sure that the types used with it are consistent for type safety. Generics provide type safety, but without any loss of performance or code bloat. While they are similar to templates in C++ in this regard, they are very different in their implementation.

The System.Collections.Generics namespace contains the generics collections in .NET 2.0. Various collections/container classes have been "parameterized." To use them, simply specify the type for the parameterized type and off you go. See Example 2:

Example 2. Type-safe generic List


List aList = new List();
aList.Add(3);
aList.Add(4);
// aList.Add(5.0);
int total = 0;
foreach(int val in aList)
{
total = total + val;
}
Console.WriteLine("Total is {0}", total);

For more information on generics.

More FAQs later.

No comments: