8.09.2012

OOPs Difference FAQs-4

1.Difference between String and StringBuilder

S.No
String
StringBuilder
1
String class belongs to the namespace System.
StringBuilder class belongs to the namespace System.Text.
2
String class is immutable. Immutable means that the string cannot be changed. Consider the following example:
class sampleClass {
public static void Main() {
string sampleStr = "Hai";
sampleStr = "Hello";
Console.WriteLine(sampleStr);
}
}
Output of this code will be:
Hello
In this example, you have created a string called sampleStr. You have initially assigned the value "Hai". And then you try to overwrite its value with "Hello". You get the overwritten value as output. But the problem lies in the number of strings that get created in memory. When you create the string as "Hai", this string gets created in the memory. When you try to change the value to "Hello", instead of overwriting the existing "Hai" string it will create a new string in the memory and assign this new string "Hello" to sampleStr.

StringBuilder class is mutable. Consider the following example:
class sampleClass {
public static void Main() {
StringBuilder sampleSB = new
StringBuilder("Hai",10);
sampleSB = new
StringBuilder("Hello");
Console.WriteLine(sampleSB);
}
}
Output of this code will be:
Hello
In this example, you are doing the same thing. But the string "Hai" will be overwritten as "Hello" and no new strings will be created in the memory.

3
You can directly assign a string to string class instance. For example,
String sampleStr = "Hai" is valid.
You cannot directly assign a string to StringBuilder instance. For example,
StringBuilder sampleSB = "Hai" will lead to the following error:
"cannot implicitly convert type 'string' to 'System.Text.StringBuilder' "
You can assign a string to StringBuilder using the following statement:
StringBuilder sampleSB = new
StringBuilder("Hai");
4
String concatenation is done using + operator. Here is an example:
class sampleClass {
public static void Main() {
string sampleStr = "Hello!";
sampleStr += " Good Day!";
Console.WriteLine(sampleStr);
}
}
Output of this code will be:
Hello! Good Day!
Here you have used += operator to perform both concatenation and assignment using single operator. You can also use + and = separately as shown below:
sampleStr = sampleStr + " Good Day!";


String concatenation is done using Append method. Here is an example:
class sampleClass {
public static void Main() {
StringBuilder sampleSB = new
StringBuilder("Hello!");
sampleSB.Append("Good
Day!");
Console.WriteLine(sampleSB);
}
}
Output of this code will be:
Hello! Good Day!
5
During string concatenation, additional memory will be allocated.
During string concatenation, additional memory will be allocated if and only if the string buffer's capacity is reached.
6
During string concatenation, additional memory will be allocated if and only if the string buffer's capacity is reached.
If the number of concatenations to be done is random or not known, then it is recommended to use stringBuilder
7
You cannot set a limit (specifying how many strings can be concatenated) to a string object using string class.
You can set a limit to StringBuilder using the member called capacity which will by default have the value 16. You can override it to any number. The maximum value acceptable is equivalent to MaxValue of Int32. If you feel that you do not want to reserve 16 as the capacity then you can very well redefine it. However the capacity will dynamically grow based on the number of strings that you append.
Here is an example demonstrating the usage of capacity:
class sampleClass {
public static void Main() {
StringBuilder sampleSB = new
StringBuilder();
Console.WriteLine(
sampleSB.Capacity);
sampleSB.Capacity = 1;
Console.WriteLine(
sampleSB.Capacity);
sampleSB.Append("str1");
sampleSB.Append("str2");
Console.WriteLine(
sampleSB.Capacity);
}
}
Output of this code will be:
16
1

8

2.Difference between Delegate and Interface

S.No
Delegate
Interface
1
Delegates can only be methods. Here is an example:
delegate void sampleDelegate();

Interface can include both properties and methods.
Here is an example for an interface: interface ItestInterface
{
int paraml {
get; set; }
void sampleMethod();
}
2
Delegate can be applied to only one method at a time
When a class implements an interface, it can implement all the methods associated with it
3
You can use a delegate that is visible in your scope
You can use an interface only when your class or struct implements it
4
Within a class, you can implement the same delegate any number of times. Assume that either sampleClass1 or sampleClass2 of Examplel includes a method called sampleMethod2( ) with the same signature as that of delegate, then the same delegate can be used to access both sampleMethod() as well as sampleMethod2( )
Within a class, you can implement an interface method only once. In Example2, interface ITestInterface has a method called sampleMethod(). When sampleClass1 implements ITestInterface it implements sampleMethod() only once. If not, then it will end up in error.
5
Delegate can implement any method that shares the same signature as that of the delegate
When an interface method is implemented, same method name and signature has to be overridden
6
Delegate is mainly used for handling events
Interfaces are not used for handling events
7
You need not bother about the other methods available in the class.You are concerned about only the method that matches delegate signature.
When a class implements an interface, though the class requires only one method it has to implement all the methods of the interface
8
To access a method using delegate, you need not require any access to the instance of the class where the method is defined
To access the method, you need an instance of the class which implements the interface or you need an interface reference pointing to the method implemented by the class
9
You can access anonymous methods using delegates
You cannot access anonymous methods.Only named methods declared in interface can be accessed by the implementing class.
10
When you call a method using a delegate, all the method pointers associated with the delegate will be scanned through before the method execution. This is not a direct method call as you assume. It has a considerable performance overhead.
When you are calling a method using interface reference, you are directly accessing the method of the class that implements the interface. This is a direct method call and it doesn't have any overhead.
11
Delegates can wrap methods of sealed classes.Sealed classes are those which cannot be inherited.
Accessing sealed types is not permissible in interface.
12
Delegates can wrap any method matching its signature irrespective of which ever class the method belongs to
Class can implement any number of interfaces and it should override only the methods belonging to those interfaces
13
Delegates can wrap static methods. Examplel discussed above has used the delegate to wrap a static method called sampleMethod()
This provision is not available with interfaces .
14
Delegate cannot involve in inheritance.
Interface can inherit other interfaces. When a class implements that interface, it has to implement all the methods belonging to the interface and its inherited interfaces as well.
Here is an example of an interface inheriting from other interfaces: interface IInterface: IInterface,1 IInterface2
{
void sampleMethod1();
void sampleMethod2();
s}



Example1: Using Delegate
delegate void sampleDelegate( );
class sampleClass1{
static void sampleMethod( ) {
Console.WriteLine(“Executing sampleMethod of sampleClass1”);
}
}
class sampleClass2 {
static void Main( ) {
sampleDelegate dele1 = new sampleDelegate(sampleClass1.sampleMethod);
dele1();
}
}

Example2: Using Interface
interface ITestInterface{
void sampleMethod( );
}
class sampleClass1 : ITestInterface {
void sampleMethod( ) {
Console.WriteLine(“Executing sampleMethod of sampleClass1”);
}
}
class sampleClass2 {
static void Main( ) {
sampleClass1 obj1 = new sampleClass1( );
obj1.sampleMethod( );
}
}




3.Difference between Virtual and Abstract keywords in .NET


S.No
Virtual
Abstract
1
If you feel that the derived class may or may not override the base class method, then you will define the base class method as virtual. Consider the following example:
namespace Application1 {
public class virtualClass {
public virtual void virtualMethod(){
Console.WriteLine("Virtual Method..");
}
}
public class derivedClass:virtualClass{
public override void virtualMethod(){
Console.WriteLine("Overridden..");
}
}
public class testClass {
public static void Main() {
virtualClass obj = new virtualClass();
obj.virtualMethod();
derivedClass dObj = new
derivedClass();
dObj.virtualMethod();
}
}
}
Output of this code will be:
Virtual Method..
Overridden..
But if you want to enforce that derived class must override the base class method then you will define the base class method as abstract.
namespace Application1 {
public abstract class abstractClass {
public abstract void abstractMethod();
}
public class derivedClass:abstractClass{
public override void abstractMethod(){
Console.WriteLine("Overridden..");
}
}
public class testClass {
public static void Main() {
derivedClass obj = new
derivedClass();
obj.abstractMethod();
}
}
}
Output of this code will be:
Overridden..
2
Virtual methods need not be compulsorily overridden. In the above example if the derived class does not override the method virtualMethod, then again the code will work.
Abstract methods should compulsorily be overridden by the derived class. In the above example, if the derivedClass does not override abstractMethod, then during compilation you will get the following error:
'Application1.derivedClass' does not implement inherited abstract member 'Application1.abstractClass.abstractMethod()'
3
To define a base class method to be virtual, you need not include any new definition for the base class. In the earlier example, you can see that the class virtualClass just includes an access modifier followed by the class name. No other additional modifiers are required.
If you want to define a method as abstract in the base class then the base class should also be marked as abstract. Consider the following example:
namespace Application1 {
public class abstractClass {
public abstract void abstractMethod();
}
}
In this example, abstractClass has an abstract method. Hence the class in itself has to be marked abstract. But it is not done in the above example. Therefore during compilation, you will end up in the following error:
'Application1.abstractClass.abstractMethod()' is abstract but it is contained in nonabstract class 'Application1.abstractClass'
4
Virtual method can have a method body. In the earlier example, virtualMethod of virtualClass has method definition like any of the other methods that you define and it is perfectly legal.
Abstract methods have only the signature. It cannot have method body. However the abstract class can include non-abstract methods and these methods can have method body defined. Consider the following example:
namespace Application1 {
public abstract class abstractClass {
public abstract void abstractMethod(){
Console.WriteLine("Abstract
method..");
}
}
}
Here you are trying to include method body for an abstract method. This is not permissible. Hence during compilation you will end up in the following error:
"'Application1.abstractClass.abstractMethod()' cannot declare a body because it is marked abstract"
5
Class containing virtual method can be instantiated. Here is an example:
namespace Application1 {
public class virtualClass {
public virtual void virtualMethod(){
Console.WriteLine("Virtual Method..");
}
}
Public class testClass {
public static void Main() {
virtualClass obj = new virtualClass();
obj.virtualMethod();
}
}
}
Output of this code will be:
Virtual Method…
Class containing abstract method cannot be instantiated. It can only be inherited. Consider the following example:
namespace Application1 {
public abstract class abstractClass {
public abstract void abstractMethod();
}
public class testClass {
public static void Main() {
abstractClass obj = new abstractClass();
}
}
}
Here you are trying to create an instance of abstract class. This is not possible. Hence during compilation you will end up in the following error:
"cannot create an instance of the abstract class or interface Application1.abstractClass"
6
Not just methods, virtual modifier can also be used with properties.
Apart from class and method, the modifer abstract can be associated with properties, events and indexers.
7
There is a restriction when using virtual modifer. You cannot use this modifer along with static or abstract or override modifiers.
Abstract modifiers also have a restriction. They cannot be used along with static or virtual or override modifiers.

4 comments:

  1. Excellent.
    Pls arrange your recent posts as tree view in your blog template. It will be easier for readers.

    ReplyDelete