10X Sale
kh logo
All Courses

Introduction

C# is a modern, object-oriented programming language developed by Microsoft as part of the .NET platform. It is used to develop applications for the Windows operating system and is often used with the .NET Framework or .NET Core. It can also be used for cross-platform development on platforms such as macOS and Linux using .NET Core. The C# interview questions and answers are curated for beginner and advanced professionals. The questions comprise various topics like array, syntax, data types, operators, asynchronous programming, web development using C# and ASP.NET. Our set of C# interview questions will help your interview preparation more effective and that will help you be confident in the interview.

C# Interview Questions and Answers
Beginner

1. Features of C# 7

C#7.3 is the current version of C#. It is a multi-paradigm, generic and object-oriented programming languages. The following are the features:

  • Positional Arguments

Named arguments can be followed by positional arguments.

  • Ref local variables

Easily reassign ref local variables in C# 7.3

  • Stackalloc Arrays

Use initializers on stackalloc arrays.

  • Dispatch

Implement method dispatch on properties other than the object type.

  • Out Variables

A method’s out parameters can be defined directly in the method in C# 7.

  • Local Functions

You can define a function within the scope of another function. This gets local variables into the scope.

  • Deconstruction

This includes deconstruction of tuples into separate variables and creating metadata for the names of tuple elements.

  • Dummy Variables

Discards are dummy variables intentionally unused in application code. The maintainability of the code and make its intent more clear.

  • Digit Separators

These are beneficial in grouping digits in large numeric literals. These provide great readability and no significant downside.

  • Binary Literals

These are literals in binary form. They are identical to hexadecimal literals except they use digits 0 and 1 with base 2. Binary literals are great for educational purposes and are low cost implementations.

2. Constant vs Readonly in C#

The const in C# is faster than readonly, but the performance of const is not better than readonly.

  • Const

A value with const keyword cannot change throughout the life of a program.

A const variable cannot be reassigned.

  • Readonly

A value with Readonly keyword whose value can be initialized during runtime, unlike const.

Let us now see the difference:

Basis
ConstReadonly
Local VariableCan be applied to local variables.Cannot be applied to local variables
MemoryMemory is not allocated.Dynamic memory is allocated for readonly fields.
StaticConst are by-default staticTo make it class member, include static keyword before readonly
Ref or outWe cannot pass const field as ref or out parameterThe readonly field can be passed as ref or out parameters in the constructor context.
FasterFaster than readonly.Readonly isn’t faster.
PerformancePerformance isn’t better than readonly.Performance is better than const.

3. Why is f required while declaring C# floats?

This is a frequently asked question in C# interview questions for freshers.

It is a suffix set in C# that is used while declaring float. This is used to inform the compiler the type of the literal. An example:

float a = 1239f;

4. What are class instances in C#?

An object of a class is a class instance in C#. All the members of the class can be accessed using the object or instance of the class.

The definition of the class instance starts with the class name that is followed by the name of the instance. Then the new operator is used to create the new instance.

A program that demonstrates a class instance in C# is given as follows:

using System;
namespace Demo
{
  class Sum
  {
    private int x;
    private int y;
    public void value(int val1, int val2)
    {
        x = val1;
        y = val2;
    }
    public int returnSum()
    {
        return x + y;
    }
   }
   class Test
   {
     static void Main(string[] args)
     {
        Sum obj = new Sum();   
        obj.value(8, 5);
        Console.WriteLine("The sum of 8 and 5 is: {0}" , obj.returnSum());
     }
   }
}

The output of the above program is given as follows:

The sum of 8 and 5 is: 13

5. How to empty an array in C#?

An array can be emptied using the Array.Clear() method. This method sets the required range of the elements in the array to their default values where the array type may be integer, boolean, class instances etc. To empty the whole array, the range of elements should simply be the length of the whole array.

A program that demonstrates the Array.Clear() method to empty the array is given as follows:

using System;
public class Demo
{
   public static void Main()
   {
       int[] arr = new int[] {3, 9, 7, 1, 6};
       Console.Write("The original integer array is: ");
       foreach (int i in arr)
       {
           Console.Write(i + " ");
       }
       Array.Clear(arr, 0, arr.Length);
       Console.Write("\nThe modified integer array is: ");
       foreach (int i in arr)
       {
           Console.Write(i + " ");
       }
   }
}

The output of the above program is given as follows:

The original integer array is: 3 9 7 1 6
The modified integer array is: 0 0 0 0 0

Want to Know More?
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Description

C# is a widely used programming language in the IT industry. Microsoft develops it, and it is mostly used for building desktop applications. More recently, C# has been used in developing Windows 8/10 applications.

Based on the survey conducted by Stackoverflow, C# is known as the 4th most popular programming language, with 31% of the developers using it regularly. Since its demand is rising in the market, it is necessary today to have a strong understanding of this language to land any dream job in the software industry.

If you are looking to make your career in the software industry as a developer, these best interview questions in C sharp will be useful in cracking the interview. To learn more about C-Sharp Courses, enroll in our course and strengthen your fundamental core. Below is a list of the most asked C# interview questions and answers. These C# interview questions can be useful for freshers and experienced professionals to clear the interview. To make you industry ready with more knowledge of programming, join our programming training course and prepare yourself confidently for the interview.

Recommended Courses

Learners Enrolled For
CTA
Got more questions? We've got answers.
Book Your Free Counselling Session Today.