C# Interview Questions (part-2)
Explain object pool in C#?
Object pool is used to track the objects which are being used in the code. So object pool reduces the object creation overhead.
What is static constructor?
Static constructor is used to initialize static data members as soon as the class is referenced first time.
Difference between this and base ?
"this" represents the current class instance while "base" the parent.
What's the base class of all exception classes?
System.Exception class is the base class for all exceptions.
How the exception handling is done in C#?
In C# there is a "try… catch" block to handle the exceptions.
Why to use "using" in C#?
The purpose of the "using" statement in C# is to guarantee the timely disposal of an object as soon as it goes out of scope, without necessitating explicit code to accomplish this. By employing the "using" statement, the Dispose() method is automatically called when the execution leaves the using-block, even in scenarios where an exception is thrown within the block. This ensures proper resource management and cleanup, promoting efficient memory usage and preventing resource leaks. The "using" statement offers a convenient and concise way to handle disposable objects, enhancing code readability and maintainability while mitigating the risk of resource-related issues.
What is a collection?
A collection works as a container for instances of other classes. All classes implement ICollection interface.
Can finally block be used without catch?
Yes, it is possible to have try block without catch block by using finally block. The "using" statement is equivalent try-finally.
How do you initiate a string without escaping each backslash?
You put an @ sign in front of the double-quoted string.
Can we execute multiple catch blocks in C#?
No. Once any exception is occurred it executes specific exception catch block and the control comes out.
What does the term immutable mean?
The data value may not be changed.
What is Reflection?
Reflection allows us to get metadata and assemblies of an object at runtime.
What namespaces are necessary to create a localized application?
Can you return multiple values from a function in C#?
Yes, you can return multiple values from a function using the following approaches:
- ref / out parameters
- Struct / Class
What is the difference between ref and out parameters?
"ref" tells the compiler that the object is initialized before entering the function, while "out" tells the compiler that the object will be initialized inside the function.
Differences | Value Types and Reference Types?
In C#, a Value Type is responsible for storing data within its own allocated memory space, while a Reference Type maintains a pointer to another memory location where the actual data resides. This distinction implies that a Value Type directly holds the assigned value itself, rather than memory addresses or references, ensuring self-contained storage. On the other hand, Reference Types store memory addresses that point to the actual data value, allowing for more complex data structures and sharing of data between multiple instances. Understanding the difference between Value Types and Reference Types is crucial for effective memory management and selecting the appropriate data storage approach in different programming scenarios.
More about.... value types and reference types
Differences constant and readonly variables in C#?Constants
- Can be assigned values only at the time of declaration
- Static by default
- Known at compile time
- Must have set value, by the time constructor exits
- Are evaluated when instance is created
- Known at run time
Mention the assembly name where System namespace lies in?
What is the .NET datatype that allows the retrieval of data by a unique key?
What is Global Assembly Cache (GAC)? , and where is it located?
The Global Assembly Cache (GAC) is a dedicated folder within the Windows directory that serves as a centralized repository for storing .NET assemblies. These assemblies are intentionally designated to be shared and accessible by all applications running on a given system. By storing assemblies in the GAC, they can be effectively shared across multiple applications without the need for redundant copies or versioning conflicts. This centralized location ensures easy accessibility and seamless integration of commonly used assemblies, promoting code reusability, modularity, and efficient memory utilization.
By default you could see all assemblies installed in GAC in
Which method do you use to enforce garbage collection in .NET?
System.GC.Collect() forces garbage collector to run. This is not recommended but can be used if situations arise.
See more.... How to force Garbage Collection
See more... What is Garbage Collection
Why do I get errors when I try to serialize a Hashtable?
XmlSerializer will refuse to serialize instances of any class that implements IDictionary.
How can I stop my code being reverse-engineered from IL?
To enhance the security of your code, it is advisable to employ code obfuscation techniques. One popular tool for code obfuscation is Dotfuscator, which offers a free edition and is conveniently bundled with Visual Studio. These obfuscation tools function by optimizing the Intermediate Language (IL) code in a manner that significantly increases the difficulty of reverse-engineering. Additionally, consider hosting your service in a reputable cloud service provider, as this offers an added layer of protection against reverse-engineering attempts. By using these measures, you can safeguard your code from unauthorized access and intellectual property theft, preserving the integrity and confidentiality of your software assets.
What is the use of Configuration Files?
The configuration file primarily serves as a means to manage settings during the deployment of an application. It proves particularly valuable in handling versioning concerns related to .NET components, ensuring seamless compatibility. Additionally, the configuration file often houses connection strings, offering the flexibility to deploy an application with the ability to connect to different test or staging servers. However, it is crucial to note that once the application is deployed in a production environment, these connection strings are typically not modified. The configuration file primarily functions as a static reference for deployment-time settings, enhancing maintainability and facilitating efficient management of application configurations in various deployment scenarios.
How Reference types and Value types are stored in memory ?
In C#, value types are stored on the stack, sharing the same process memory space, while reference types are stored on the heap. Reference types, such as objects, arrays, and classes, have their actual data stored on the heap, with the memory address of the object being stored on the stack as a value type. This memory allocation distinction between value types and reference types is crucial for understanding how data is stored and accessed within the memory hierarchy of a program. By comprehending these storage mechanisms, developers can make informed decisions regarding memory management and optimize performance in their code.
How do you convert a string into an integer in .NET?
What is a formatter?
A formatter is an essential component in the data processing workflow, with the crucial responsibility of encoding and serializing data into messages on one end, and deserializing and decoding messages into data on the other end. This process of encoding and serialization involves converting data from its native format into a standardized representation suitable for transmission or storage. Similarly, the deserialization and decoding phase involves the reverse operation, where encoded messages are transformed back into their original data format for further processing and utilization. By employing formatters, developers can ensure seamless interoperability and effective communication between various components and systems, enabling efficient data exchange and integration.
How many classes can a single .NET DLL contain?
One DLL can contains Unlimited classes.
What is manifest?
It is the metadata that describes the assemblies
Difference between int and int32 ?
Both are same. System.Int32 is a .NET class. Int is an alias name for System.Int32.