Vous êtes sur la page 1sur 8

1.Difference between String and StringBuilder S.No 1 2 String String class belongs namespace System.

to StringBuilder the StringBuilder class belongs to the namespace System.Text. 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.

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.

You can directly assign a string to You cannot directly assign a string to string class instance. For example, StringBuilder instance. For example, String sampleStr = "Hai" is valid. 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"); String concatenation is done using 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!";

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!

During string concatenation, During string concatenation, additional additional memory will be memory will be allocated if and only if allocated. the string buffer's capacity is reached. During string concatenation, If the number of concatenations to be additional memory will be done is random or not known, then it is allocated if and only if the string recommended to use stringBuilder buffer's capacity is reached. 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 1 Delegate Interface

Delegates can only be methods. Interface can include both properties Here is an example: and methods. delegate void sampleDelegate(); Here is an example for an interface: interface ItestInterface { int paraml { get; set; } void sampleMethod(); } Delegate can be applied to only When a class implements an interface, one method at a time it can implement all the methods associated with it You can use a delegate that is You can use an interface only when visible in your scope your class or struct implements it 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.

3 4

Delegate can implement any When an interface method is method that shares the same implemented, same method name and signature as that of the delegate signature has to be overridden Delegate is mainly used for Interfaces are not used for handling handling events events

You need not bother about the other methods available in the class.You are concerned about only the method that matches delegate signature. To access a method using delegate, you need not require any access to the instance of the class where the method is defined

When a class implements an interface, though the class requires only one method it has to implement all the methods of the interface 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

You can access anonymous You cannot access anonymous methods using delegates methods.Only named methods declared in interface can be accessed by the implementing class. 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.

10

11

Delegates can wrap methods of Accessing sealed types sealed classes.Sealed classes are permissible in interface. those which cannot be inherited. Delegates can wrap any method matching its signature irrespective of which ever class the method belongs to

is

not

12

Class can implement any number of interfaces and it should override only the methods belonging to those interfaces

13

Delegates can wrap static This provision is not available with methods. Examplel discussed interfaces . above has used the delegate to wrap a static method called sampleMethod() Delegate cannot inheritance. involve in 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();

14

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 1

Virtual 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.."); }

Abstract 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 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.. 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.

} } public class testClass { public static void Main() { derivedClass obj = new derivedClass(); obj.abstractMethod(); } } } Output of this code will be: Overridden..

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.abstractMe thod()' 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.abstractMe

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.

thod()' 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.abstractM ethod()' cannot declare a body because it is marked abstract" 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"

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

Not just methods, virtual modifier Apart from class and method, the

can also be used with properties. 7 There is a restriction when using virtual modifer. You cannot use this modifer along with static or abstract or override modifiers.

modifer abstract can be associated with properties, events and indexers. Abstract modifiers also have a restriction. They cannot be used along with static or virtual or override modifiers.

And, further updates on difference between questions and answers, please visit my blog @ http://onlydifferencefaqs.blogspot.in/

Vous aimerez peut-être aussi