Friday, April 14, 2023

Top 100 .NET framework and C# interview questions for beginners (part-1)

1. What is .NET Framework?

.NET Framework is a software development framework created by Microsoft that provides a programming model for building applications for Windows. It includes a large class library called the Framework Class Library (FCL) and a Common Language Runtime (CLR) that provides services such as memory management, type safety, and exception handling. 

.NET Framework supports several programming languages, including C#, Visual Basic, and F#. It also supports the development of a wide range of applications, such as desktop applications, web applications, and mobile applications.


2. What is C#?

C# (pronounced "C sharp") is a modern, object-oriented programming language designed by Microsoft. It was created specifically to develop applications for the .NET Framework, and it is now widely used for developing a variety of applications on various platforms, including desktop, mobile, and web applications. 

C# is similar to other object-oriented programming languages such as Java and C++, and it includes a number of powerful features such as garbage collection, type safety, and support for advanced language constructs such as lambdas and async/await.


3. What is Common Type System (CTS)?

Common Type System (CTS) is a part of the .NET Framework that defines the data types and programming constructs that can be used in all .NET programming languages. It provides a common set of rules for how data types are defined and used across different programming languages that target the .NET runtime. This allows programs written in different languages to seamlessly interoperate with each other. 

The CTS specifies the types that can be used in a .NET application, including value types, reference types, classes, interfaces, and delegates. It also defines how these types are represented in memory and how they interact with each other.


4. What is Common Language Runtime (CLR)?

Common Language Runtime (CLR) is the heart of the .NET Framework. It is responsible for managing the execution of .NET code, providing features such as memory management, security, exception handling, and type safety. When a .NET application is executed, the CLR takes care of loading the necessary assemblies, verifying the safety of the code, compiling it into machine code, and executing it.
The CLR provides a managed execution environment that abstracts away many of the low-level details of computer hardware and operating systems. This makes it possible for .NET code to be written once and run on any platform that has a compatible implementation of the CLR installed, including Windows, Linux, and macOS.

5. What is managed code?

Managed code is code that is written in a language such as C#, VB.NET, or F# and is executed by the Common Language Runtime (CLR) in the .NET Framework. When code is compiled into Microsoft Intermediate Language (MSIL) by the .NET Framework compiler, it is managed code. The CLR provides services such as garbage collection, security, and exception handling for the managed code. This allows the developer to focus on writing the application logic instead of worrying about low-level details such as memory allocation and deallocation. The CLR also ensures that managed code runs in a safe and controlled environment.

6. What is unmanaged code?

Unmanaged code refers to code that is executed directly by the operating system without the assistance of a runtime environment. This type of code is typically written in native programming languages such as C or C++ and is compiled into machine code that can be directly executed by the operating system. Since unmanaged code does not run in a managed runtime environment like .NET, it is responsible for managing its own memory and resources, which can lead to issues like memory leaks and buffer overflows.

7. What is the difference between managed and unmanaged code?

Managed code is code that is written to run on a virtual machine, such as the .NET Framework's Common Language Runtime (CLR), and is executed under the control of the CLR. Managed code is compiled to Intermediate Language (IL) and then Just-in-Time (JIT) compiled to native code at runtime.


On the other hand, unmanaged code is code that is written in a programming language that does not have automatic memory management, such as C or C++. Unmanaged code does not run under the control of a virtual machine or managed runtime environment, and it is typically compiled directly into machine code that can be executed by the operating system.


The main difference between managed and unmanaged code is that managed code runs in a managed runtime environment, which provides automatic memory management, security, and other features that help to improve code reliability, while unmanaged code does not have these features and requires manual memory management and other low-level programming constructs.


8. What are attributes in .NET?

In .NET, attributes are used to provide additional information about types, methods, properties, and other code elements at compile time or runtime. Attributes can be added to a code element by using the square bracket syntax, like [AttributeName].

Attributes can be used to specify metadata that can be used by compilers, build tools, or other tools that process code. For example, the [Serializable] attribute can be used to indicate that a class can be serialized, and the [Obsolete] attribute can be used to mark a code element as deprecated.

Attributes can also be used to define custom behavior in .NET applications. For example, the [DllImport] attribute is used to specify that a method is implemented in an external DLL, and the [WebMethod] attribute is used to mark a method as a web service method.

Attributes can be accessed at runtime using reflection, which allows developers to inspect metadata about types and members at runtime.


9. What is garbage collection?

Garbage collection is the automatic memory management process in .NET that automatically deallocates the memory of objects that are no longer being used by an application. The garbage collector periodically scans the managed heap for objects that are no longer being referenced by any part of the application, and frees the memory allocated by these objects.


Garbage collection relieves the developer from the responsibility of manually deallocating memory, which can be error-prone and time-consuming. Instead, the garbage collector automatically tracks object usage and memory allocation, allowing the developer to focus on writing the application logic without worrying about memory management.


10. What is the purpose of the IDisposable interface?

The IDisposable interface is used to provide a way for an object to release unmanaged resources. It declares a single method named Dispose that releases these resources when called.


When a class implements the IDisposable interface, it is indicating that it is using unmanaged resources that need to be explicitly released. The Dispose method can be called explicitly by the programmer or automatically by the garbage collector when the object is no longer being used.


Implementing IDisposable allows the object to control when the unmanaged resources are released, which can be important for performance and stability of the application. It is typically used for objects that interact with system resources like files, network sockets, or database connections.



11. What is a delegate?

In C#, a delegate is a type that represents a reference to a method. It allows methods to be passed as parameters to other methods or assigned to variables, making it useful for creating callbacks and event handling mechanisms. A delegate can be thought of as a function pointer in C/C++, but with the added benefits of type safety and object-oriented programming.


Delegates can be used to create a loosely-coupled architecture, where components can communicate with each other without having to know the specifics of the other components. They can also be used to implement the observer pattern, where an object (the subject) maintains a list of its dependents (observers) and notifies them automatically of any state changes.



12. What is an event?

In C#, an event is a mechanism that allows an object to notify other objects when something happens. It is a way of providing a simple way for one object to inform a list of interested parties when something of interest happens. An event is declared in a class using the event keyword, and can be subscribed to by other objects using the += operator.


In order to raise an event, the class that declares it needs to call the event handler method associated with the event. The event handler method is defined by other classes that have subscribed to the event. The class that raises the event is known as the event publisher, while the class that handles the event is known as the event subscriber.


Events are commonly used in graphical user interfaces to notify the application of user actions such as button clicks, mouse movements, and keyboard input. They are also used in other scenarios such as network communication, where a connection can notify a listener of changes in state.



13. What is a callback?

A callback is a technique in programming where a function or method is passed as an argument to another function or method, which is then called back or invoked at a later time. The main purpose of using callbacks is to allow for asynchronous execution of code and to enable communication between different parts of the program. 

When a callback function is passed as an argument, it can be executed at a later point in time, either synchronously or asynchronously, based on the requirements of the program. The use of callbacks is common in event-driven programming, such as in GUI programming, where the program needs to respond to user actions or events in real-time.


14. What is a lambda expression?

A lambda expression is a shorthand notation for defining a delegate or an anonymous method in C#. It provides a concise way to create anonymous functions, which can be used in place of delegates in many situations.


Lambda expressions are also known as anonymous functions because they do not have a method name or return type. Instead, they consist of a list of parameters, followed by the "=>" operator and the body of the function.


Here's an example of a lambda expression that takes two integers as parameters and returns their sum:

(int x, int y) => x + y;


This lambda expression can be assigned to a delegate or used directly in a method call that expects a delegate. It can also be used with LINQ to provide a concise syntax for filtering, sorting, and projecting collections of objects.



15. What is a closure?

A closure is a programming construct in which a function can reference variables from its outer (enclosing) scope, even after the outer function has returned. This means that the function can "remember" the values of those variables and use them later. Closures are often used to create functions that act as callbacks, or to encapsulate functionality in a private scope. In C#, closures are typically created using lambda expressions or anonymous methods.


16. What is an anonymous method?

An anonymous method is a method without a name. It is a way to define and use a method in C# without declaring it as a separate method with a name. Anonymous methods are often used as a convenient way to define event handlers or to pass code as a parameter to a method.
 

Anonymous methods are defined using the delegate keyword and are used to implement a delegate type. 

The syntax for an anonymous method is as follows:
delegate (parameters)
{
   // code to be executed
}

Here, parameters is an optional list of parameters, and // code to be executed is the code that will be executed when the delegate is invoked.


Anonymous methods were introduced in C# 2.0 and are now mostly replaced by lambda expressions, which provide a more concise syntax for defining inline code blocks.


17. What is the difference between abstract class and interface?

An abstract class and an interface are two ways to define a contract for classes to follow in C#. Here are the main differences between the two:

  1. Implementation: An abstract class can have both abstract and non-abstract (concrete) methods, while an interface can only have abstract methods.
  2. Multiple inheritance: A class can only inherit from one abstract class, but it can implement multiple interfaces.
  3. Access modifiers: Abstract classes can have different access modifiers for their members, while interfaces can only have public members.
  4. Fields: An abstract class can have fields, while an interface cannot.
  5. Versioning: Adding a new method to an interface will break all the classes implementing that interface, while adding a new method to an abstract class will not break any existing subclasses.



18. What are the access modifiers in C#?

In C#, access modifiers are keywords used to specify the accessibility or visibility of a class, method, field, or property. The access modifiers in C# include:

  1. public: The public keyword allows access to the class, method, field, or property from any code in the project or assembly.
  2. private: The private keyword limits access to the class, method, field, or property to only the containing class.
  3. protected: The protected keyword allows access to the class, method, field, or property only within the containing class or its derived classes.
  4. internal: The internal keyword allows access to the class, method, field, or property from within the same assembly or project.
  5. protected internal: The protected internal keyword allows access to the class, method, field, or property within the containing class, its derived classes, and within the same assembly or project.

These access modifiers provide control over the accessibility of code elements, which is essential for encapsulation and maintaining the integrity of the code base.


19. What is a sealed class?

In C#, a sealed class is a class that cannot be inherited by other classes. The sealed keyword is used to prevent the class from being used as a base class for another class.


Once a class is marked as sealed, it cannot be inherited. This means that you cannot create a derived class from a sealed class.


sealed classes are often used to prevent unintended modifications to the behavior of the class. For example, if a class is designed to perform a specific function and should not be modified, it can be marked as sealed to prevent other developers from extending or modifying the class.


It is important to note that sealed classes can still be instantiated and used like any other class, but they cannot be inherited.



20. What is the purpose of the volatile keyword?

In C#, the volatile keyword is used to indicate to the compiler that a variable's value can be changed by external factors at any time, and therefore the variable cannot be cached. This ensures that the value of the variable is always retrieved from memory and not from a cache, which could be stale.


The volatile keyword is typically used when multiple threads need to access the same variable, to prevent any caching of the variable's value by any thread.


Without the volatile keyword, the compiler may optimize the code by caching the value of the variable in a register or on the stack, which can lead to unexpected behavior when multiple threads are accessing the variable.


.NET framework and C# interview questions for beginners (part-1)

.NET framework and C# interview questions for beginners (part-2)

.NET framework and C# interview questions for beginners (part-3)

.NET framework and C# interview questions for beginners (part-4)

.NET framework and C# interview questions for beginners (part-5)



No comments:

Post a Comment

Please keep your comments relevant.
Comments with external links and adult words will be filtered.