C Sharp Interview Questions and Answers for Freshers, Experienced


C# is a type-safe, managed and object oriented language, which is compiled by .Net framework for generating intermediate language (IL).

Below are some of the features supported in C# -

  • • Methods
  • • Properties
  • • Passing Parameters
  • • Arrays
  • • Main
  • • Collections
  • • Constructors and Destructors
  • • Indexers
  • • Objects
  • • Classes
  • • Resilience to change
  • • Data abstraction
  • • Encapsulation(Wrapping of data)
  • • Inheritance
  • • Polymorphism
  • • Modularity
  • • Message passing

An object is an instance of a class through which we access the methods of that class.

"New" keyword is used to create an object.

A class that creates an object in memory will contain the information about the methods, variables and behavior of that class.

In simple Words A class is a concept.

public class TechTrick
{
}
TechTrick TT = new TechTrick();

1.Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.

2. Abstraction is the concept of hiding irrelevant details. In other words make complex system simple by hiding the unnecessary detail from the user.

Abstraction solves the problem in the design level. Where as Encapsulation solves the problem in the implementation level.

The method of constructing one class from another is called Inheritance. The derived class inherits all the properties and methods from the base class and it can add its own methods also.

public class A
{
}
public class B : A
{
}

The word Polymorphism means having many forms. Generally, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.

In other words, "Many forms of a single object is called Polymorphism."

There are two types of Polymorphism:

  • • Compile Time polymorphism (Function Sequence,Method Overloading and Operator Overloading)
  • • RunTime polymorphism (Method Overriding)
  • • Array stores the values or elements of same data type but arraylist stores values of different datatypes.
int arr[] = new int[10]
  • • Arrays will use the fixed length but arraylist does not uses fixed length like array.
ArrayList arrList = new ArrayList();

The array which has elements of type array is called jagged array. The elements can be of different dimensions and sizes. We can also call jagged array as Array of arrays.

int[][] Marks = new int[5][];
Marks[0] = new int[2];
Marks[1] = new int[4];
Marks[2] = new int[5];
Marks[3] = new int[8];
Marks[4] = new int[10];
Marks[0] = new int[] { 4, 5, };
Marks[1] = new int[] { 2, 0, 2, 4};
Marks[2] = new int[] { 1,2, 0, 2, 4};
Marks[3] = new int[] { 1, 0, 2, 4, 6, 45, 67, 78 };
Marks[4] = new int[] { 34, 54, 67, 87, 78 ,1, 0, 2, 4, 6};
  • • "Const" keyword is used for making an entity constant. We cannot modify the value later in the code. Value assigning is mandatory to constant variables. Constant variables are declared and initialized at compile time.
const int RollNo = 11;
  • • "readonly" variable value can be changed during runtime and value to readonly variables can be assigned in the constructor or at the time of declaration.
readonly int RollNo = 11;

"Static" keyword can be used for declaring a static member. If the class is made static then all the members of the class are also made static. If the variable is made static then it will have a single instance and the value change is updated in this instance.

Sealed class is used to prevent the class from being inherited from other classes. So "sealed" modifier also can be used with methods to avoid the methods to override in the child classes.

sealed class SealedClass
{
}

Structs are value-type variables and classes are reference types. Structs stored on the stack, causes additional overhead but faster retrieval. Structs cannot be inherited.

struct TechTrick
{
string MyProperty { get; set; }
}
class TechTrick
{
string MyProperty { get; set; }
}
  • • System.String is immutable. When we modify the value of a string variable then a new memory is allocated to the new value and the previous memory allocation released.
string s = string.Empty;
  • • System.StringBuilder was designed to have concept of a mutable string where a variety of operations can be performed without allocation separate memory location for the modified string.
StringBuilder sb = new StringBuilder ();
  • • Using Clone() method, we creates a new array object containing all the elements in the original array.
  • • Using CopyTo() method, all the elements of existing array copies into another existing array.
  • • Both the methods perform a shallow copy.
  • • Dispose() is called when we want for an object to release any unmanaged resources with them.
  • • On the other hand Finalize() is used for the same purpose but it doesn’t assure the garbage collection of an object.
  • • "is" operator is used to check the compatibility of an object with a given type and it returns the result as Boolean.
if (someObject is StringBuilder)
  • • "as" operator is used for casting of object to a type or a class.
StringBuilder b = someObject as StringBuilder;

"out" parameter can be passed to a method and it need not be initialized where as "ref" parameter has to be initialized before it is used.

int x;
TechTrick(out x); // OK
int y;
TechTrick(ref y); // Error: y should be initialized before calling the method
  • • enum keyword is used for declaring an enumeration, which consists of named constants and it is called as enumerator lists.
  • • Enums are value types in C# and these can’t be inherited.
enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };

Partial classes concept added in .Net Framework 2.0 and it allows us to split the business logic in multiple files with the same class name along with "partial" keyword.

public interface ISignup
{
//ISignup related function
}
public interface ISignIn
{
//ISignIn related function
}
//UserSignup.cs file
public partial User : ISignup, ISignIn
{
//implements ISignup interface
}
//UserSignIn.cs file
public partial User
{
//implements SignIn interface
}

When we want to give permission to a derived class to override a method in base class, Virtual keyword is used.

public virtual void show()
{
Console.WriteLine("Hello: Base Class!");
Console.ReadLine();
}

A constructor is a member function with the same name as its class. The constructor is invoked whenever an object of its associated class is created.It is called constructor because it constructs the values of data members of the class.

class Sum
{
int a, b;
public Sum() //default contructor
{
a = 50;
b = 25;
}
}

Destructor is a special method that get invoked/called automatically whenever an object of a given class gets destroyed. Main idea behind using destructor is to free the memory used by the object.

class destructor
{
~destructor(){}
}

Garbage Collection is a process of releasing memory automatically occupied by objects which are no longer accessible.

Early binding and late binding are the concept of polymorphism. There are two types of polymorphism in C#.

  • • Compile Time Polymorphism: It is also known as early binding.
  • • Run Time Polymorphism: It is also known as late binding or method overriding or dynamic polymorphism.

A base class is a class that is used to create, or derive other classes. Classes derived from a base class are called child classes, subclasses or derived classes. A base class does not inherit from any other class and is considered parent of a derived class.

  • • An Abstract method does not provide an implementation and forces overriding to the deriving class (unless the deriving class also an abstract class), where as the virtual method has an implementation and leaves an option to override it in the deriving class.
  • • Thus Virtual method has an implementation & provides the derived class with the option of overriding it. Abstract method does not provide an implementation & forces the derived class to override the method.
Abstract class :-
  • Abstract class can have abstract and non-abstract methods.
  • Abstract class doesn't support multiple inheritance.
  • Abstract class can have final, non-final, static and non-static variables.
  • Abstract class can provide the implementation of interface.
  • The abstract keyword is used to declare abstract class.

The abstract keyword is used to declare abstract class.

Example:
public abstract class Shape
{
public abstract void draw();
}

Interface :-
  • Interface can have only abstract methods. it can have default and static methods also.
  • Interface supports multiple inheritance.
  • Interface has only static and final variables.
  • Interface can't provide the implementation of abstract class.
  • The interface keyword is used to declare interface.
Example:
public interface Drawable
{
void draw();
}

"Finally" block will be executed irrespective of exception. So while executing the code in try block when exception is occurred, control is returned to catch block and at last "finally" block will be executed. So closing connection to database / releasing the file handlers can be kept in "finally" block.

Variable types does not hold null values so to hold the null values we have to use nullable types. So nullable types can have values either null or other values as well.

int? num1 = null;
int? num2 = 45;
double? num3 = new double?();

Nullable Coalescing Operator can be used with reference types and nullable value types. So if the first operand of the expression is null then the value of second operand is assigned to the variable. For example,

double? myFirstno = null;
doudoubleble mySecno;
mySecno = myFirstno ?? 10.11;
  • • Generics in c# is used to make the code reusable and which intern decreases the code redundancy and increases the performance and type safety.
  • • Namespace – "System.Collections.Generic" is available in C# and this should be used over "System.Collections" types.

Method overloading is creating multiple methods with the same name with unique signatures in the same class. When we compile, the compiler uses overload resolution to determine the specific method to be invoke.

static void Foo(int x)

Console.WriteLine("Foo(int x)");
}
static void Foo(string y)
{
Console.WriteLine("Foo(string y)");
}
static void Main()

Foo("text");
}

  • • In method overriding, we change the method definition in the derived class that changes the method behavior.
  • • Method overloading is creating a method with the same name within the same class having different signatures.

Delegates are type safe pointers unlike function pointers as in C++. Delegate is used to represent the reference of the methods of some return type and parameters

Below are the list of uses of delegates in C# -

  • • Callback Mechanism
  • • Asynchronous Processing
  • • Abstract and Encapsulate method
  • • Multicasting

Thread is an execution path of a program. Thread is used to define the different or unique flow of control. If our application involves some time consuming processes then it’s better to use Multithreading,which involves multiple threads.

Below are the states of thread :-
  • • Unstarted State
  • • Ready State
  • • Not Runnable State
  • • Dead State
There are three types of Caching :-
  • • Output Caching: stores the responses from an asp.net page.
  • • Fragment Caching: Only caches/stores the portion of page (User Control)
  • • Data Caching: is Programmatic way to Cache objects for performance.

When we want to transport an object through network then we have to convert the object into a stream of bytes. The process of converting an object into a stream of bytes is called Serialization. For an object to be serializable, it should implement ISerialize Interface. De-serialization is the reverse process of creating an object from a stream of bytes


Stay Connected

Popular Posts

Get Latest Stuff Through Email


Who Should Read TechTrick?

All the tricks and tips that TechTrick provides only for educational purpose. If you choose to use the information in TechTrick to break into computer systems maliciously and without authorization, you are on your own. Neither I (TechTrick Admin) nor anyone else associated with TechTrick shall be liable. We are not responsibe for any issues that caused due to informations provided here. So, Try yourself and see the results. You are not losing anything by trying... We are humans, Mistakes are quite natural. Here on TechTrick also have many mistakes..