Signup/Sign In

C# Interview Questions And Answers

Posted in Programming   LAST UPDATED: DECEMBER 27, 2021

    C# Interview Question

    If you're preparing for a C# interview and it's your first, being anxious is quite normal. However, reading through the questions listed below will give you a sense of what you may be asked, and you will undoubtedly feel more secure prior to your interview.

    Question 1. What Exactly is C#?

    "See Sharp" is how C# is pronounced.

    C# is a Microsoft-developed type-safe object-oriented programming language that runs on the.NET framework.

    C# allows developers to create a diverse range of safe and resilient programs, including windows client applications, client-server applications, XML web services, distributed components, and database applications.

    Question 2. How Are The Ref And Out Parameters Different?

    Before providing the ref parameter to the method, it must be initialized. This implies that when the value of this parameter is modified in the method, the calling method will be updated.

    The out parameter does not need to be initialized before being given to the function.

    code

    Question 3. What Is An Enum?

    An enumeration is a collection of named integer constants collectively referred to as an enumerator list. The "enum" keyword is used to define an enumerated type.

    Enumeration is a value data type in C#, and it cannot inherit values.

    By default, enums are private, but they may be made public.

    By default, the first enumerator has a value of 0 and each subsequent enumerator has a value of 1. For instance: Example: enum Days { Sat, Sun, Mon, Tue, Wed, Thu, Fri };

    In enum, Days Sat is 0, Sun is 1, Mon is 2, and so on. Additionally, you may modify the default values for enumerators. Additionally, we may modify the enumerator's default value.

    The enum type is used to specify static constants.

    Question 4. What Is The Difference Between Boxing and Unboxing?

    Boxing is a conversion technique that converts a value type to a reference type. This is an unintentional conversion. We may assign a value straight to an object, and C# will do the conversion for us.

    Example : int i = 10; object obj = i; // Box the integer type i into object type obj.

    Unboxing is the inverse process of boxing. Unboxing is the conversion of a reference type to a value type. This must be clearly stated. Explicitly, the object type is converted to the value type.

    Example : int i = 10; object obj = i; // Box the integer type i into object type obj. int j = (int) obj; // Unbox the integer value stored in object type obj to integer type j.

    Question 5. Which class serves as the foundation class for all of.net's data types?

    The Object class is the superclass that all other classes in the.NET Framework inherit from. It is the root of the hierarchy of types.

    Question 6. In C#, what is the '??' Operator?

    The?? operator is often referred to as the null coalescing operator. This operator is used to provide a default value for a variable of the nullable value type or the reference type. If an operand is not null, it returns the left-hand operand; otherwise, it returns the right-hand operand.rand.

    Example :

    code

    Question 7. In C#, What Is A Ternary Operator (?:)?

    Three arguments are required for a ternary operator.

    The first argument is a comparison argument, and the second argument is the value of the comparison result if it is true. And the third parameter is value if the comparison returns a false result.

    This operator is used to consolidate lines of code and improve readability.

    Example:

    int i = 10; string message; message = I < 0 ? “Negative” : “Positive”

    Question 8. Distinguish between System and System and String. Text.StringBuilder.

    This section contains a list of items that are related to the String and StringBuilder classes.

    System.String - It represents an immutable string. - An object is not altered.

    System.Text.StringBuilder - It represents a mutable string. - Because it is dynamic in nature, we have the ability to change it. - We can utilize it for a great deal of text modification. - Faster than String Class.

    Question 9. What Are The Various Kinds Of Access Modifiers In C#?

    C# supports the following five kinds of access modifiers:

    • Private – By default, this is the access modifier. A private member may be accessed only by other members of the same class or struct.
    • Protected - A protected member may be accessed only from inside the same class or a class derived from it. A protected member of a class may be defined, however, a protected member of a struct cannot be declared, since structs do not allow inheritance.
    • Public- A public member may be accessed from inside the same assembly or from any other assembly that refers it. A public member of a class or struct may be defined. By default, interface members are public, and no access modifiers may be defined for them.
    • Internal - An internal member is only accessible inside the same assembly; it cannot be accessed from any other assembly. An internal member of a class or struct may be defined. By default, when defined within a namespace, classes, structs, interfaces, and delegates have internal access. When nested, a class, struct, interface, or delegate has private access.
    • Protected Internal - A protected member may be accessed inside the same assembly or within a derived class contained within another assembly. A member of a class may be declared protected internal, but a member of a struct may not be declared protected internal, since structs do not allow inheritance.

    Question 10. What Is The Difference Between Safe And Unsafe Code in C#?

    In C#, codes are classified as safe or dangerous. The term "safe code" refers to code that runs beneath the CLR.

    The dangerous code is not managed by CRL. In other words, dangerous codes are not discarded. As a result, it is critical to handle every hazardous code individually and properly.

    We can declare a whole method, code block, or individual statement as unsafe by using the unsafe keyword in C#

    .Example:

    public unsafe void MyUnsafeMethod() {
    
    // write unsafe code here }

    Question 11. What Is The Difference Between An Object, A Variable, and A Dynamic Type?

    The following are some critical points to keep in mind:

    • Object - It is the basic class for all c# 1.0 types. - If the compiler recognizes the type of an object, it should be typecast. - The object may be provided as a method parameter or return type.
    • Var - It is required to initialize the var type variable at declaration - It may contain any kind of data - It is type-safe. It cannot be used as a parameter or return type in a method. - Typecasting is unnecessary since the compiler is aware of the type and actions of the object.
    • Dynamic-It is dynamic in nature and can store any kind of data. - It is not type-safe due to the possibility that the compiler does not know full information. - It may be provided as an argument to a method or as a return type. - Although typecasting is not required, the user should be aware of the object's type, attributes, and actions.

    Question 12. Describe The Concept Of Namespaces in C#.

    Classes are contained inside namespaces. In C#, we'll utilize namespaces to organize similar classes. The term "using" may be used to reference the namespace in other namespaces. A namespace is declared in C# using the keyword. The general syntax for a namespace and nested namespace is shown here:

    namespace namespace_name
    {
           // member namespace
           namespace namespace_name
           {
                   // members
           }
    }

    Question 13. In C#, What Is A Structure?

    A structure in C# is a value type that includes data members and members functions, similar to a class. Fields, methods, constants, constructors, properties, indexers, and operators may all be included inside the structure.

    As with a class, a struct may include methods, fields, indexers, properties, operator methods, and events, as well as nested structures.

    • A struct may have no more than one default constructor.
    • A struct is not permitted to contain destructors. Because it is a value type, there is no need to invoke the garbage collector.
    • In contrast to a class, a struct lacks inheritance but may implement one or more interfaces.
    • The struct is sealed by default and therefore cannot be inherited by another type.
    • A struct cannot contain access modifiers that are abstract, virtual, or protected. Because these access modifiers are used with base type members, a struct cannot be used as a base type.
    • In contrast to a class, a struct may be instantiated directly without the need for a new operator. In this scenario, unassigned struct fields will stay uninitialized, and the struct instance will be unavailable until all fields are populated.

    Structures are helpful for small data structures and outperform classes.

    Question 14. What If I Implement a Private Constructor?

    When we have to implement a singleton class pattern, we can declare a constructor private.

    Private constructor contains only static members. As it is a private constructor, it cannot be externally called. If a class that has one or more private constructor and no public constructor, then other classes except nested classes are not allowed to create an instance of this class and it cannot be inherited.

    Question 15. What are Sealed Classes in C#?

    Sealed classes are unique kinds of classes that are not inheritable. The sealed modifier prevents a class from inheriting. When attempting to inherit from a sealed class, a compile-time error occurs.

    Example :

    code

    Q16. So What Makes Your Code Object-Oriented?

    Ans: To grasp this, we must first assess the advantages of OO. To achieve this, we must thoroughly comprehend its base. So given that C# is at its heart object-oriented.

    Q17. What is Cohesion?

    Ans: In OOPS, we construct our code in modules. Each module has specific tasks. Cohesion reveals how much module duties are firmly connected.

    Higher cohesiveness is always preferable. Higher cohesiveness benefits are:

    • Improves maintenance of modules.
    • Increase reusability.

    Q18. Why are strings in C# immutable?

    Ans: Immutable implies string values cannot be modified after being produced. Any alteration to a string value results in an entirely new string instance, hence an inefficient use of memory and extra trash collection.

    Q19. What are the core concepts of OO programming?

    Ans: As a developer, you may be inclined to say that it contains features like Encapsulation, Polymorphism, Abstraction, and Inheritance. Although this is accurate, it doesn’t truly convey the underlying core of OO and its advantages.

    Principles are necessary, but they are not the most significant component of what OO truly is. What is exceptionally crucial is to understand on what grounds OO is based upon, or in other words, the foundations of OO programming.

    The two most essential core notions on which OO has been built in C# are this pointer and Dynamic Dispatch.

    There are concepts like Encapsulation, Polymorphism, Abstraction, and Inheritance, but they are the outcome and not the generating force behind the OO paradigm in C#.

    Q20. What is Coupling?

    Ans: OOPS Modules are dependant on each other. Coupling refers to the amount of reliance between two software units.

    Two modules are highly reliant on each other if you have changed in one module, and for supporting that modification every time, you have to alter the dependant module.

    Loose Coupling is usually desired.

    Inversion of Control and dependency injections are strategies for obtaining loose Coupling in modules.

    Q21. How are methods overloaded?

    Ans: Methods are overloaded through various signatures (number of arguments and types) (number of parameters and types). Thus, you may overload a method by having various data types, different amounts of arguments, or a different sequence of parameters.

    Q22. Explain Jagged Arrays in C#?

    Ans: If the elements of an array are an array, then it’s referred to as a jagged array. The components may be of varying sizes and proportions.

    Q23. What is the GAC, and where is it located?

    Ans: The GAC is the Global Assembly Cache. Shared assemblies live in the GAC; this enables apps to share assemblies instead of deploying the assembly with each application. Versioning permits several assembly versions to exist in the GAC—applications may define version numbers in the config file. The gacutil command-line utility is used to manage the GAC.

    Q24. What is Encapsulation?

    Ans: In non-object-oriented languages, data and actions are not bound together. That implies any function in the program may alter the data.

    In Encapsulation, we tie the data and actions into one object. Only declared actions in a class may affect the data. We conceal the status of an object by utilizing properties and methods. Clients of the object only view these actions, and clients may edit the data via only these behaviors.

    We further safeguard the data by utilizing access specifiers. We placed the private / protected keywords before data to safeguard it from the outside world.

    Q25. Write a program to reverse a string in C#?

    Ans. Below is the program to reverse a string in C#

    using System;
    namespace Learn
    
    {
    
    class Program
    
    {
    
    static void Main(string[] args)
    
    {
    
    string str = ” “, reverse = ” “;
    
    int Length = 0;
    
    Console.WriteLine(“Enter a String : “);
    
    str = Console.ReadLine();
    
    Length = str.Length – 1;
    
    while(Length>=0)
    
    {
    
    reverse = reverse + str[Length];
    
    Length–;
    
    }
    
    Console.WriteLine(“Reverse string is {0}”,reverse);
    
    Console.ReadLine();
    
    }
    
    }
    
    }

    About the author:
    Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.
    Tags:interview-questionsc#
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS