Link Search Menu Expand Document

C Sharp Interview Questions for 5 Years Experience

Q1. What do you mean by C# language?

Answer: This is a simple, type-safe object-oriented programming language from Microsoft that combines the computing power of c++ and visual basics concepts, which fulfills the user’s multipurpose objectives. It is compiled over the .NET framework and can also work on open source platforms.

Q2. Why are strings in c# immutable?

Answer: When we say immutable, we mean that it cannot be changed once defined. Same as in the case of strings. The string value cannot be changed once created. Any change in the string value results in a completely new string, thereby increasing the garbage collection and inefficient memory use. The string stores text values as a sequence of char (System. Char) elements representing Unicode characters. Usually, one char element stands for one symbol, and that value is immutable.

Q3. What is the need for Encapsulation?

Answer: it can also be termed as data hiding. It refers to the process of enclosing one or more items within a physical or logical package. It is used to hide data or structured data objects from accidental corruption, preventing unauthorized parties from accessing them directly. One can use numerous Encapsulation methods to control their code(data) from being misused without their concern.

Q4. What can be the output of the following program?

Answer: public class Program

{
        public static void Main(string[] args)
        {
            var actions = new List<Action>();
            for (int i = 0; i < 4; i++)
              actions.Add(()=>Console.WriteLine(i));
            foreach (var action in actions)
                action();
        }
    }

 

Output:

4
4
4
4

Q5. What are delegates?

Answer: Delegates in c# are the same as pointers to functions in C, C++. A delegate is a reference type variable that holds the reference to a method. The connection can be changed at runtime. Delegates are used to pass methods as arguments to other methods. It can also be chained together; for example, multiple methods can be called on a single event.

Q6. How will you implement Encapsulation?

Answer: It is the wrapping up of data under a single unit. In another way, Encapsulation is a protective shield that prevents the data from being accessed by the code outside this shield.

  • Public access specifier: This allows a class to have access from anywhere i.e.inside the class or outside the class. It gives its member variables and member functions an option to have exposure to other objects or functions. They are publicly available.
  • Private access specifier: It doesn't allow its member variables and member functions to be called outside the parent class. Only functions of the same class access private members. Even an instance of a class does not have the right to access its private members.
  • 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.

 Q7. What do you mean by Namespaces?

Answer: It organizes and provides a level of separation to the codes. Also helps to keep one set of names different from another group of names. It gives an advantage of zero clashings of the same class names in other Namespaces.

Namespace gives you the following advantages:

  • They help in organizing large code projects.

The “. “operator is used to eliminate them.

  • Specification of the name of the namespace for every class is prevented by using directives.
  • The global namespace is the "root" namespace: global::System will always refer to. NET System interface.

Other useful articles:


Back to top

© , C Sharp Online — All Rights Reserved - Terms of Use - Privacy Policy