English speaking and learning Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Sunday, November 13, 2016

C# Interview Questions and Answers Part 3

Que 1)What is difference b/w boxing and unboxing.
 Boxing is a mechanism to explicitly convert a value type to a reference type by storing the variable into System.Object; when you box the value the CLR allocates a new object into the heap and copies the value type's value into that instance. 
For example you have created a variable of int type as:
 int a = 20; object b = a; //boxing 
The opposite operation is Unboxing which is the process of converting back the reference type into the value type. This process verifies that the receiving data type is equivalent to the boxed type as;
 int c = (int)b; // unboxing 
The C# compiler sees the assignment from int to object and vice-versa. When this program is compiled and you examine the IL generated code via IL dissembler, you notice that the program respond by inserting a box instruction in the IL automatically when b is assigned the value of a and an unbox instruction when c is assigned the value b as in the following;

Que 2) What is benefits of generics?
There are some significant characteristics of Generic types that make them special to the conventional non-generics type as follows;

Type Safety
Binary Code reuse

Type Safety
One of the most significant features of Generics is Type Safety. In the case of the non-generic ArrayList class, if objects are used, any type can be added to the collections that can sometimes result in a great disaster. The following example shows adding an integer, string and object to the collection of an ArrayList type;

ArrayList obj = new ArrayList();
obj.Add(new TestClass());

Now, if the collection is iterated through the foreach statement using integer elements, the compiler accepts the code but because all the elements in the collection are not an integer, a runtime exception occurs;

foreach(int i in obj)

The rule of thumb in programming is that Errors should be detected as early as possible. With the generic class Test<T>, the generic type T defines what types are allowed. With the definition of Test<int>, only an integer type can be added to the collection. The compiler doesn't compile the code because the Add() method has invalid arguments as follows;

Test<int> obj = new Test<int>();
obj.Add("Dog");            //compiler error
obj.Add(new TestClass());  //compiler error

Another feature of Generics is performance. Using value types with non-generic collection classes result in boxing and unboxing overhead when a value type is converted to reference type and vice-versa.

In the following example, the ArrayList class stores objects and the Add() method is defined to store some integer type argument. So an integer type is boxed. When the value from ArrayList is read using the foreach statement, unboxing occurrs.

ArrayList  obj = new ArrayList(); 

obj.Add(50);    //boxing- convert value type to reference type
int x= (int)obj[0]; //unboxing
foreach(int i in obj)
   Console.WriteLine(i);   // unboxing

Note: Generics are faster than other collections such as ArrayList.

Instead of using objects, a Generics type of the TestClass<T> class is defined as an int, so an int type is used inside the class that is generated dynamically from the compiler. Therefore boxing and unboxing no longer occurs as in the following; 

TestClass<int> obj = new TestClass<int>();
obj.Add(50);    //No boxing
int x= obj[0]; // No unboxing
foreach(int i in obj)
   Console.WriteLine(i);   //No unboxing

Binary Code reuse
Generics provide a kind of source code protection. A Generic class can be defined once and can be instantiated with many different types. Generics can be defined in one CLR supported language and used from another .NET language. The following TestClass<T> is instantiated with an int and string types:

TestClass<int> obj = new TestClass<int>();
TestClass<string> obj1 = new TestClass<string>();
Que3) Difference b/w abstraction and encapsulation
Abstraction solves the problem in the design level. 
Encapsulation solves the problem in the implementation level.
Abstraction is used for hiding the unwanted data and giving only relevant data. 
Encapsulation is hiding the code and data into a single unit to protect the data from outer world.
Abstraction is set focus on the object instead of how it does it.
 Encapsulation means hiding the internal details or mechanics of how an object does something.
Abstraction is outer layout in terms of design. 
For Example: - Outer Look of a iPhone, like it has a display screen. 
Encapsulation is inner layout in terms of implementation.
For Example: - Inner Implementation detail of a iPhone, how Display Screen are connect with  each other using circuits
Que 4) what is extern in C#?
When a method declaration includes an extern modifier, that method is said to be an external method. External methods are implemented externally, typically using a  language other than C#. Because an external method declaration provides no actual 
implementation, the method-body of an external method simply consists of a semicolon. 
An external method may not be generic. The extern modifier is typically used in conjunction with a DllImport attribute, allowing external methods to be implemented by DLLs (Dynamic Link Libraries). The execution environment may support other mechanisms whereby implementations of external methods can be provided. When an external method includes a DllImport attribute, the method declaration must also include a static modifier.

extern alias X;
extern alias Y;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
    class Extern
        public static extern int MessageBox(int h, string m, string c, int type);

        public static int  Main()

            X.Class1.A._b = 1;
            Y.Class1.A._b = 2;

            Console.Write(X.Class1.A._b );
            string myString;
            Console.Write("Enter your message: ");
            myString = Console.ReadLine();
            return MessageBox(0, myString, "My Message Box", 0);
Que 5)what is dynamiv keyword in C#?
The dynamic type enables the operations in which it occurs to bypass compile-time type checking. Instead, these operations are resolved at run time. The dynamic type simplifies access to COM APIs such as the Office Automation APIs, and also to dynamic APIs such as IronPython libraries, and to the HTML Document Object Model (DOM).
Type dynamic behaves like type object in most circumstances. However, operations that contain expressions of type dynamic are not resolved or type checked by the compiler. The compiler packages together information about the operation, and that information is later used to evaluate the operation at run time. As part of the process, variables of type dynamic are compiled into variables of type object. Therefore, type dynamic exists only at compile time, not at run time.
you also like this blog.
1)Abstraction and Encapsulation in OOPS

2)Inheritance in OOPS

3)Polymorphism in OOPS

4)Interface in OOPS

5)What is Virtual Function

6)What is Abstract class and Abstract function

7)What is Static Class and Static Members

8)What is Collections


  1. dùng dầu gội tresemme có tốt không để có thể sử dụng cho tóc yếu, chăm sóc tóc một cách tốt nhất. Trên thị trường hiện nay có rất nhiều loại thuốc nhuộc cao cấp, hay dùng thử thuốc nhuộm davines nhé, thuốc nhuộm tóc davines giá bao nhiêu và mua ở đâu là tốt nhất?
    Có thể bạn quan tâm rằng cửa kính tự động giá bao nhiêu để có thể lựa chọn cho mình một đơn vị cung cấp và lắp đặt tốt nhất. chúng tôi chuyên cung cấp các sản phẩm cửa tự động cortech hàn quốc chính hãng tốt nhất. Điều đặc biệt hơn, Nhật Bản một đất nước nổi tiếng với nhiều thương hiệu cửa tự động chất lượng cao, tham khảo cổng trượt tự động nhật bản để có một sự lựa chọn tốt nhất.