C# Interview Questions & Answers

  • Home
  • Interview_Question

Interview Questions & Answers



Select Series



What is C#?

C# is a modern, general-purpose, object-oriented programming language developed by Microsoft. C# is designed for Common Language Infrastructure (CLI), which consists of the executable code and runtime environment that allows use of various high-level languages on different computer platforms and architectures.

In how many ways you can pass parameters to a method?

There are three ways that parameters can be passed to a method − Value parameters − This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. Reference parameters − This method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument. Output parameters − This method helps in returning more than one value.

Can you return multiple values from a function in C#?

Yes! Using output parameters. A return statement can be used for returning only one value from a function. However, using output parameters, you can return two values from a function.

What is namespace in C#?

A namespace is designed for providing a way to keep one set of names separate from another. The class names declared in one namespace does not conflict with the same class names declared in another.

What is the purpose of using statement in C#?

using keyword is used to include a namespace in the program. A program generally has multiple using statements.

What are reference types in C#?

The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables. In other words, they refer to a memory location. Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value. Example of built-in reference types are: object, dynamic, and string.

Which class acts as a base class for all the data types in .net?

The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). Object is an alias for System.Object class. The object types can be assigned values of any other types, value types, reference types, predefined or user-defined types. However, before assigning values, it needs type conversion.

What is encapsulation?

Encapsulation is defined 'as the process of enclosing one or more items within a physical or logical package'. Encapsulation, in object oriented programming methodology, prevents access to implementation details.

What is scope of a public member variable of a C# class?

Public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class.

What is scope of a protected member variable of a C# class?

Protected access specifier allows a child class to access the member variables and member functions of its base class. This way it helps in implementing inheritance.

What is scope of a Internal member variable of a C# class?

Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly. In other words, any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined.

How to sort an array in C#?

Using Array.sort(array) function. It sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.

How to sort an array in C# in descending order?

First sort the array using Array.sort(array) then reverse the same using Array.reverse(array) method.

What is a structure in C#?

In C#, a structure is a value type data type. It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating a structure. Structures are used to represent a record. To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member for your program.

What are the differences between a class and structure?

Classes and Structures have the following basic differences − 1-Classes are reference types and structs are value types. 2-Structures do not support inheritance. 3-Structures cannot have default constructor.

What is the default access for a class member?

Default access for the members is private.

What is inheritance?

One of the most important concepts in object-oriented programming is inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and speeds up implementation time. When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class. The idea of inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well, and so on.

Is multiple inheritance supported in C#?

No! C# does not support multiple inheritance.

What is polymorphism?

The word polymorphism means having many forms. In object-oriented programming paradigm, polymorphism is often expressed as 'one interface, multiple functions'.

What is the difference between static polymorphism and dynamic polymorphism?

Polymorphism can be static or dynamic. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.