1. What is Managed or Unmanaged Code?
“The code, which is developed in .NET framework is known as managed code. This code is directly executed by CLR with the help of managed code execution. Any language that is written in .NET Framework is managed code”.
The code, which is developed outside .NET framework is known as unmanaged code.
“Applications that do not run under the control of the CLR are said to be unmanaged, and certain languages such as C++ can be used to write such applications, which, for example, access low – level functions of the operating system. Background compatibility with the code of VB, ASP and COM are examples of unmanaged code”.
2. What is Boxing and Unboxing?
Boxing and Unboxing both are used for type conversion but have some difference:
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.
private void boxing()
int _iLocal = 170;
object _obj = _iLocal;//boxing
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.
private void unboxing()
object _obj = 170;
int _iLocal = (int)_obj;//unboxing
3. What is the difference between a struct and a class in C#?
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.
4. What is the difference between Interface and Abstract Class?
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.
- 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 declare or use any variables while an interface is not allowed to do so.
- 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 use constructor while in an interface we don’t have any type of constructor
- 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.
- An abstract class can’t be used for multiple inheritance while interface can be used as multiple inheritance.
5.What is the difference between string and StringBuilder in c#?
StringBuilder and string both use to store string value but both have many differences on the bases of instance creation and also for performance:
String is an immutable object. Immutable like when we create string object in code so we cannot modify or change that object in any operations like insert new value, replace or append any value with existing value in string object, when we have to do some operations to change string simply it will dispose the old value of string object and it will create new instance in memory for hold the new value in string object like:
private void stringFun()
string str = string.Empty;
str += "Welcome";// creata a new string instance rather then using old one
str += " to desire2code.com";
Point to remember:
- It’s an immutable object that hold string value.
- Performance wise string is slow because it’s create a new instance to override or change the previous value.
- String belongs to System namespace.
Stringbuilder is mutable object which also hold the string value, mutable means once we create a System.Text.Stringbuilder object we can use this object for any operation like insert value in existing string with insert functions also replace or append without creating new instance of System.Text.Stringbuilder for every time so it’s use the previous object so it’s work fast as compare than System.String. Let’s have an example to understand System.Text.Stringbuilder like:
private void stringBuilderFun()
StringBuilder str = new StringBuilder("");
str.Append("Welcome");// Will use it's old instance
str.Append(" to desire2code.com");
Point to remember:
- StringBuilder is a mutable object.
- Performance wise StringBuilder is very fast because it will use same instance of StringBuilder object to perform any operation like insert value in existing string.
- StringBuilder belongs to System.Text.Stringbuilder namespace.