C# Programming Technical Interview Questions

What is C#?

C# (pronounced as C sharp) is a type of programing language, which has been built on the characteristics of the C Programming language. It is a general-purpose, object-oriented programming (OOP) language that aims to provide high-level features for program organization. As both a successor of C and yet being an OOP language, it can be coded in both ways, hence making it a hybrid language.

C# adds a collection of pre-defined classes to the original C, which can be utilized multiple times. It also enables the overloading of certain operators such as addition and includes several other operators such as comparison, arithmetic, bit manipulation, logical operators, etc. It also incorporates essential concepts such as polymorphism, virtual and friend functions, templates, namespaces and pointers.

C# is popularly utilized as with system/application software and drivers, client-server applications, as well as with embedded firmware.

What is the reason behind the invention of C#?

C# was invented by a team at Microsoft led by Anders Hejlsberg. Even though C#, is derived from C and C++, it was developed as an individual language from the ground up.

The intention behind developing C# was to create a more powerful and flexible programming language than C and C++. The team sought to add features of Object oriented programming (OOP) to the language yet keep it small and flexible. In fact, C# was originally named COOL, which stood for "C-like Object Oriented Language". Microsoft had wanted to keep this name, but it was not feasible to trademark.

C# removes some of the complexities and pitfalls of other OOP languages, including Java and C++. This includes the removal of macros, templates, multiple inheritance, and virtual base classes. These were chosen to be removed as they causing confusion or leading to potential problems for C++ developers.

However, at its core, C# is still quite similar to C and C++, and even to Java to some extent. Many aspects are taken directly C and C++, but improvements have been made to make the language simpler. One such improvement included eliminating redundancies, as well changes to the syntax.

What are the main reasons to use C# language?

C# was developed from the C and C++ programming language. Hence, it intended to be an improvement over these two. C# is a simple and elegant programming language that is type-safe. It is an object-oriented programming language (OOP), unlike C, and thus has many characteristics of OOP, which C does not.

C# is developed by Microsoft, and thus its primary usage is to write .NET applications. It also supports existing code integration via COM/Platform support. It has native garbage collection and allows the treatment of class-methods' signatures as free functions and leading to more dynamic and flexible relationships between classes.

C# also has a huge standard library of functions that are easy to use and implement. It also allows native code blocks, as well as easily remedy of DLL problems through assembly versioning.

C# is also highly portable especially for those that are familiar with C and C++.

What are the differences between C and C#?

C# was originally developed by borrowing concepts from C and C++. However, it was built up from the ground up as a separate language. The primary difference between the two programming languages is the fact that while C is a procedure oriented programming language, C# adds the features of object-oriented programming language (OOP) to it, thus making it a multi-paradigm programming language.

C

C#

C is a procedure oriented programming language.

C# is a multi-paradigm programming language as it has concepts of both procedure oriented programming and object-oriented programming.

Top down approach

Bottom up approach

More suitable for system programming apps, hardware apps, embedded device and chip designing

Suitable for apps development and web apps development

Deals with four basic data type like int, float, double and char.

Deals with five data types: int, float, double, char, and Booleans.

32 keywords

87 keywords

One type of integral

Two type of integrals: char type and whole number which can be signed or unsigned.

Can be compiled on any platform

Can be compiled primarily on Windows

What are the differences between C++ and C#?

C# was developed as an independent language by Microsoft. However, it borrowed heavily from C and C++ programming languages. While C is a procedure oriented programming language, C++ is an object oriented programming language, whereas C# is a multi-paradigm programming language as it has concepts of both procedure oriented programming and object-oriented programming.

C++

C#

Developed from C

Developed from C and C++

Low level and platform neutral programming language

High level language that is component oriented

When compiled, C++ code is converted directly into assembly language code

When compiled, C# code is converted into Intermediate language code and then converted into executable code through Just-In-Time compilation

Allocated memory has to be manually deleted

Memory management automatically handled by garbage collector

In Switch Statement, the test variable cannot be a string.

In Switch Statement, the test variable can be a string.

When break statement is not given, the fall through will happen to the next case statement even if the current case statement has any code.

When break statement is not given, the fall through will not happen to the next case statement if the current case statement has any code

Does not contain ‘for each’ statement

Contains ‘for each’ statement

Structs behave like classes

Structs can contain only value types

The default access to Structs is public

Structs is sealed and cannot have a default no-argument constructor

Delegates, events and properties cannot be specified as class members.

Delegates, events and properties can be specified as class members.

The end of the class definition has a closing brace followed by a semicolon

The end of the class definition has just a closing brace

The access modifiers are public, private, protected. It does not have internal and protected internal access modifiers

The access modifiers are public, private, protected, internal and protected internal

Does not have finally block in exception handling mechanism.

Has finally block in exception handling mechanism.

The exception can throw any class.

The exception can only throw a class that is derived from the System.

Has the concept of function pointers

Does not have the concept of function pointers

Has a concept called Delegates

Does not have the concept of Delegates

What is the difference between public, static and void?

Public, static and void are three different modifiers that can be used in C#. each one serves a different purpose.

Public – The public modifier is the most liberal modifier in C#. It is a type of access modifiers, which can be accessed from anywhere inside or outside the class. Public modifiers are public, i.e. not private. They is no access restriction on a public modifier. The public keyword is used just before the class keyword to declare class as public.

Static – The static method is a method that is used to declare that the Main method is a global one. Hence, it can be used and called without a need to create an instance of that class. The static class can be addressed by the using the class name followed by the operator and method name. The compiler stores the static method’s address and uses it before any object is created for execution.

Void –Void is a type modifier. It states that that the Main method does not return any value.

What are constructors in C#?

In C#, a constructor is a type of method that creates an instance of a class. It is automatically executed when an object of that class that has a constructor is created or instantiated. A constructor is also called an overload constructor. A constructor has the same name as the as the class or struct. Constructors also usually initialize the data members of the new object.

The benefit of having a constructor is that it guarantees that the object will go through a proper initialization before it is used. It also allows for pre-initializing of some of the class variables with values before the object is used.

Constructors can either be static or an instance. They also do not have any return type.

What are the different types of constructors in C#?

In C#, a constructor is a method type that is automatically created with an instance of a class. There are 5 types of constructors:

1. Default Constructor

A default constructor is a type of constructor without any parameters. Basically, it does not take parameters. The default constructor initializes all numeric fields in the class to zero, and all string and object fields to null. With a default constructor, every instance of the class will be initialized to the same values and it is not possible to initialize each instance of the class to different values.

2. Parametrized Constructor

A parametrized constructor is a constructor with at least one parameter. It allows one to initialize each instance of the class to different values.

3. Copy Constructor

The copy constructor creates an object by copying variables from another object. It basically initializes a new instance to the values of an existing instance.

4. Static Constructor

The static constructor can be invoked only once for all of instances of the class. It can be invoked only during the creation of the first instance of the class or the first reference to a static member in the class. The static constructor is primarily used to initialize static fields of the class. It is also used write the code that needs to be executed only once. It also cannot access modifiers or have parameters. It also cannot be called directly.

5. Private Constructor

A private constructor is created with a private specifier. As it is private, is not possible for other classes to derive from this class. It is also not possible to create an instance of this class. Private constructors are usually used in classes that contain static members only.

What is a static constructor?

A static constructor is a type of constructor that can be invoked only once for all of instances of the class. It can only be invoked only during the creation of the first instance of the class, or it can be invoked at the first reference to a static member in the class.

The static constructor is typically used to initialize the static fields of the class and to write code that only needs to be executed once. A static constructor cannot take access modifiers or have parameters. It cannot be called directly and the user has no control on when the static constructor is executed within the program.

Static constructors are primarily used when the class is using a log file. Here, the constructor can be used to write entries to this file.

What is method overloading in C#?

In C#, method overloading occurs when one uses the same method name with different type of parameters or different set of parameters. It can also be said that creating multiple methods in a class with same name but different parameters and types is called as method overloading.

C# supports a lot of default or optional parameters. However, there is no facility to declare optional parameters as preferred by the user. Hence, there are times when the same method name must be used to facilitate different functions. In order to do that, the method name must then be set to different parameter. When this is used, it is known as method overloading.

One common example of method overloading is compile time polymorphism which is done at compile time.

Is overriding of a function possible in the same class?

It is typically not possible to override a method in the same class as overriding is a concept of inheritance which exposes the child class functionalities rather than base class. Hence it has to be in child class.

However, C# does support method overriding, but only if explicitly requested using the modifiers override and virtual. When overriding one method with another, the signatures of the two methods must be identical and must have the same visibility. Using these modifiers, class methods, indexers, properties and events can all be overridden in C#.

What is array?

An array stores the same collection of data. It is typically used to store a fixed-size sequential collection of elements of the same type. Basically, it a collection of variables of the same type that are stored at contiguous memory locations. Instead of declaring individual variables, an array can be used to represent individual variables by declaring one array variable such as numbers.

In C#, an array can be declared by using the following syntax:

datatype[] arrayName;

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.