News:

MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids

Main Menu

New C# / OOP Questions & Answers

Started by Kalyan, Apr 26, 2008, 01:05 PM

Previous topic - Next topic

Kalyan

New C# / OOP Questions

Q. You mark a method in a class as protected internal. What is the access level of this method?

protected internal will allow the method to be accessible only from derived classes in the same assembly as the base class. internal means that the method is accessible to only the classes within the same assembly and protected further restricts that to derived classes.

Q. What is the difference between an an abstract class and an interface?

An abstract class may be fully implemented whereas an interface is an abstract
set of members with no implementation.

Q. How would you decide whether to use an abstract class or an interface?

There is no one correct answer, but some of the considerations are:

  * If you need to partially or fully implement some of the methods/properties,
then use an abstract class. Interface cannot have implementation.

  * If the methods are properties you are creating apply to a diverse set of
different types of unrelated objects, then an interface is more appropriate.
Abstract classes are more suitable for related object types.

  * If the methods/properties are subject to change frequently, then an abstract
class is the one to use. Interfaces, once implemented cannot be changed - you
have to create a new interface.

Q. What is the value of string s1 after the following code is run:

string s1 = "string 1";
string s2 = s1;
string s2 = "string2";

Value of string s1 will be "string1". Even though strings are reference types,
changing s2 does not change s1 because string are immutable - i.e. once created
they cannot be changed. Changing the value of a string effectively creates a new
string.

Q. What is the difference between a string and a StringBuilder? Which one would
you use for large number of string concatenation operations and why?

String is a reference type and holds string data types. StringBuilder is also a reference type ,specifically designed to overcome the inefficiencies of doing string concatenation using the regular "+" operator. By definition strings are immutable, i.e. once created they cannot be changed - so every time you concatenate string you are creating new string objects.

This can be a very inefficient  process. StringBuilder allows you to concatenate (via the Append method) strings very efficiently string. So for large number of string concatenation operations you would use a StringBuilder.