C# Interview Questions and Answers

woman-wearing-red-blazer-3201696
Photo by cottonbro from Pexels

Below can be found the top interview questions and answers concerning C#, catering for both professional C# developers and beginners alike.

1. C# – what is it?

Developed in 2000 by Microsoft, C# is a programming language for computers. It was designed to be a multi-purpose programming challenge to enable the use of various kinds of software for all sorts of platforms such as Mobile, Web, and Windows from just one language. It is regarded as amongst the most popular languages for programming today worldwide. It is utilized by millions of software developers in the building of numerous kinds of software.

Microsoft .NET software application-building is the primary use of the C# programming language. It is used by Developers for the building of practically all types of software Developers can build almost every kind of software using C# including Android apps, native IoS, Web apps, server-free applications, controls, and Web services, controls and libraries, cloud API’s, backend services, console apps, Window UI apps, blockchain applications, machine learning software, and AI.

Combined with Visual Studio IDE, C# offers a speedy development for applications. It provides a high performance, object-focussed, simple to use and versatile modern programming language. Its origins are found in the best use cases and aspects of other languages for programming such as SmallTalk, Pascal, Java, and C++.

Syntax-wise, C# is similar to C++.Net whilst its library resembles that of Java. C# is compatible with many contemporary object-based language features in programming such as Inheritance, Polymorphism, Encapsulation, and Abstraction. As a highly typed language, the Object class inherits the majority of language types.

Concepts of classes and objects are also supported by C# – the classes are made of members (i.e. methods, events, properties, and fields). For more information, check out this article on OOP and C#.

Modern programming needs are ideally catered for by the versatility of C# which has seen many upgrades in versions – the most up to date being v8.0.

2. Definition of an object in C#? 

Classes are the foundation of C# which is, to reiterate, an object-based programming language. Classes are templates that determine the look of a data structure, how the data itself is to be transferred, managed, and stored. The class is made up of methods, properties, and fields amongst other members.

Objects are real whilst classes are concepts – the former requires class instances to be created and their type is defined by the class. Real values are stored in the computer memory by Objects.

Object refers to an actual real entity that can carry out work and which bears certain characteristics. We refer to this also as an instance (objects being instances of classes), that is to say, an entity copy in given programming.

Imagine, for instance that we need a program set up to manage motorcycles. An entity needs to be created for the motorcycle – so the class is Motorcycle. This will have four properties, say, namely, size, color, type, and model.

We, therefore, to represent our car in programming language, create a class named Motorcycle with the above four properties. We call these properties members of a class. There are types of members, namely events, delegates, methods, properties, and fields. In turn, you can have a public, private, or protected class member. A class member can be private, protected, and public. In this case, we are talking about the public class members as they are accessible from outside of the class.

3. Managed or Unmanaged Code? 

Managed Code

Any code that is developed with the .NET framework or the related programming languages that it supports (e.g. VB.NET or C#) is known as “Managed code”. Its execution relies directly on the CLR (Common Language Runtime or Runtime). The Runtime handles the code lifecycle, from the creation of objects, through the allocation of memory to disposal of objects. Managed code also refers to any code written in.NET Framework.

Unmanaged Code

Any code, however, developed independently of the .NET framework is referred to as unmanaged code. Similarly, unmanaged also applies to any applications outside of CLR’s control. We understand here programming languages like Visual Basic, C++, or C.

It is up to the programmers to handle the creation, execution, and disposal of objects in unmanaged code. Memory leaks and undesired allocation of resources may result from the writing of bad code by programmers.

A mechanism exists within The .NET Framework to use unmanaged code and managed code to be used within each other –for this process to work requires wrapper classes. 

4. Boxing and Unboxing in C#? 

Type conversions require the usage of boxing and unboxing.

Boxing refers to the process by which a reference type is converted from a value type. This conversion is known as implicit. Below an example of boxing in C#.

  1. // Boxing  
  2. int anum = 123;  
  3. Object obj = anum;  
  4. Console.WriteLine(anum);  
  5. Console.WriteLine(obj); 

Unboxing, conversely, refers to the process by which a reference type converts to a value type. Below an example of this:

  1. // Unboxing  
  2. Object obj2 = 123;  
  3. int anum2 = (int)obj;  
  4. Console.WriteLine(anum2);  
  5. Console.WriteLine(obj);  

5. A struct or a class in C#? 

Whilst Structure and Class are both types that are user-defined, there are significant distinctions between them:

Struct:

  • In C# this is a value type and inherits from System Value Type.
  • It is normally utilized in the case of smaller data quantities.
  • Cannot inherit from other types
  • Cannot be abstract
  • It does not require the creation of a new object with a new keyword.
  • It does not have the authorization to create default constructors.

Classes:

  • In C#, it is a reference type, inheriting from System. Object Type.
  • Normally used for large quantities of data
  • Cannot inherit from other classes.
  • It cannot be an abstract type.
  • It can be used for the creation of default constructors.

6. What is the difference between Interface and Abstract Class in C#? 

Here are some of the common differences between an interface and an abstract class in C#.

  • 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 have 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 members or functions are private by default while in an interface all are public, we can’t change them manually.
  • In an abstract class, we need to use abstract keywords to declare abstract methods, while in an interface we don’t need to use that.
  • An abstract class can’t be used for multiple inheritances while the interface can be used as multiple inheritances.
  • An abstract class use constructor while in an interface we don’t have any type of constructor.

7. What is enum in C#? 

A value type that comes with a set of related named constants, commonly known as an enumerator list, is referred to as an enum. An enumeration is declared by using the enum keyword. Enum is a user-defined primitive data type.

It can exist as an integer (float, int, byte, double, etc.). However, it needs to be cast if using it adjacent to an int.

Creating numeric constants in the .NET framework requires enum, whose members are all type enum. All enum types require a numeric value.

Int. is the underlying type, by default of the enumeration element. The first value of an enumerator is 0 by default, increasing by 1 per successive enumerator.

enum Dow {Sat, Sun, Mon, Tue, Wed, Thu, Fri};    

Points to bear in mind about Enum,

  • Enums are enumerated data types in C#.
  • Enums are aimed at developers not end-users.
  • Enums are strongly typed constants, i.e. one type of enum won’t necessarily be assigned to another enum’s type, regardless of whether the value of their underlying members is identical.
  • Your code will be more understandable and readable thanks to Enumerations (enums).
  • Values for Enum are fixed. Enum and processed as an integer and can be displayed as a string.
  • Int. is the default type whilst the approved types include byte, sbyte, short, ushort, uint, long, and ulong.
  • System Enum is where all enum types automatically derive from, thus allowing us to use System. Enum methods on enums.
  • As value types, enums are created not on the heap but on the stack.