C# Questions and Answers
C# is the best language for writing Microsoft .NET applications. C# provides the rapid application development found in Visual Basic with the power of C++. Its syntax is similar to C++ syntax and meets 100% of the requirements of OOPs like the following:
Boxing is the process of converting a value type data type to the object or to any interface data type which is implemented by this value type. When the CLR boxes a value means when CLR is converting a value type to Object Type, it wraps the value inside a System.Object and stores it on the heap area in application domain.
Unboxing:Unboxing is also a process which is used to extract the value type from the object or any implemented interface type. Boxing may be done implicitly, but unboxing have to be explicit by code.
Class and struct both are the user defined data type but have some major difference:
The struct is value type in C# and it inherits from System.Value Type.
Struct is usually used for smaller amounts of data.
Struct can’t be inherited to other type.
A structure can't be abstract.
No need to create object by new keyword.
Do not have permission to create any default constructor.
The class is reference type in C# and it inherits from the System.Object Type.
Classes are usually used for large amounts of data.
Classes can be inherited to other class.
A class can be abstract type.
We can’t use an object of a class with using new keyword.We can create a default constructor.
We need to create an instance of a System.Threading.Thread object and passing it an instance of a ThreadStart delegate that will be executed on the new thread.
public MyDemoThread( string initData )
m_data = initData;
m_thread = new Thread( new ThreadStart(ThreadMain) );
} // ThreadMain() is executed on the new thread.
private void ThreadMain()
Console.WriteLine( m_data );
public void WaitUntilFinished()
private Thread m_thread;
private string m_data;
Theoretically their are some differences between Abstract Class and Interface which are listed below:
A class can implement any number of interfaces but a subclass can at most use only one abstract class.
An abstract class can have non-abstract methods (concrete methods) while in case of interface all the methods has to be abstract.
An abstract class can declare or use any variables while an interface is not allowed to do so.In an abstract class all data member or functions are private by default while in interface all are public, we can’t change them manually.
In an abstract class we need to use abstract keyword to declare abstract methods while in an interface we don’t need to use that.
An abstract class can’t be used for multiple inheritance while interface can be used as multiple inheritance.An abstract class use constructor while in an interface we don’t have any type of constructor.
System.String is immutable.
Append keyword is not used.
System.String is slow.
System.StringBuilder is mutable.
Append keyword is used.System.StringBuilder is fast.
|constructor chaining is a way to connect two or more classes in a relationship as Inheritance, in Constructor Chaining every child class constructor is mapped to parent class Constructor implicitly by base keyword so when you create an instance of child class to it’ll call parent’s class Constructor without it inheritance is not possible.|
|An interface class is an abstract class with public abstract methods. You can inherit the class and have the methods over-ridden.|
C# doesn't support multiple inheritances while C++ does.
- We can use switch statement with string values in C# while in C++ only Character and integer values supported.
- Casting is Safer in C# than C++.
- C# doesn’t require semicolon on completion of class definition while C++ use.- In C#, Command line parameter acts differently as compared to C++.
The classes contained in the System.Globalization namespace are useful for writing globalized (internationalized) applications. It contains classes that define culture-related information such as language, the country/region, the calendars in use, the format patterns for dates, currency, and numbers, and the sort order for strings.The System.Resources namespace contains classes and interfaces which allow to create, store, and manage various culture-specific resources used in an application.