| Static vs Singleton :👈 | 👉:C# Sharp QA-17 |
C# (C-Sharp) Question Answers - 151-160 |
151. Difference between IEnumerable and IQueryable?
IEnumerable executes in-memory; IQueryable builds expression trees for deferred execution (e.g., LINQ to SQL).
152. Use of Span<T> in C# 13?
Provides high-performance, memory-safe access to contiguous memory regions without allocations.
153. Conceptually, what is the difference between early-binding and late-binding?
Early binding means the compiler determines the type, method, and member references at compile time. The compiler can verify that the referenced members exist and generate direct calls.
Late binding means the type or member to invoke is determined at runtime. The compiler does not verify that the target member exists, and resolution occurs while the application is running.
Examples:
- Early Binding: Normal method calls, interfaces, generics.
- Late Binding: Reflection, dynamic keyword, COM Interop.
Early binding offers better performance and compile-time safety. Late binding offers greater flexibility but introduces runtime overhead and potential runtime errors.
154. Is using Assembly.Load a static reference or dynamic reference?
Assembly.Load is considered dynamic loading and is generally associated with late binding because the assembly is loaded at runtime rather than being referenced at compile time.
Assembly assembly = Assembly.Load("MyPlugin");
The compiler does not verify that the assembly exists. The assembly is resolved when the application is running.
If reflection is used after loading the assembly, the interaction remains fully late-bound.
Type type = assembly.GetType("MyPlugin.Customer");
object obj = Activator.CreateInstance(type);
If the loaded type is cast to a known interface, invocation can become early-bound through that interface while the loading itself remains dynamic.
155. When would using Assembly.LoadFrom or Assembly.LoadFile be appropriate?
Assembly.LoadFrom
- Plugin architectures.
- Loading assemblies from custom folders.
- Optional modules not referenced at compile time.
Assembly.LoadFrom(@"C:\Plugins\ReportPlugin.dll");
LoadFrom participates in the assembly loading context and provides better dependency resolution for plugin scenarios.
Assembly.LoadFile
- Assembly inspection tools.
- Loading assemblies from arbitrary paths.
- Loading multiple copies or versions of assemblies.
Assembly.LoadFile(@"D:\Libraries\MyLibrary.dll");
LoadFile bypasses much of the normal assembly resolution behavior and is used less frequently.
156. What is an Assembly Qualified Name? Is it a filename? How is it different?
An Assembly Qualified Name is the fully qualified identity of a .NET type, including its namespace, type name, assembly name, version, culture, and public key token.
Example:
MyApp.Models.Customer, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
It is not a filename.
A filename might be:
MyApp.dll
An Assembly Qualified Name uniquely identifies a specific type within a specific assembly, while a filename only identifies the physical file.
157. Is this valid? Assembly.Load("foo.dll");
Technically it compiles, but it is usually incorrect.
Assembly.Load("foo.dll");
Assembly.Load expects an assembly display name, not a filename.
Correct usage:
Assembly.Load("foo");
Or:
Assembly.Load(new AssemblyName("foo"));
If you want to load using a file path, use:
Assembly.LoadFrom(@"C:\Folder\foo.dll");
or
Assembly.LoadFile(@"C:\Folder\foo.dll");
158. How is a strongly-named assembly different from one that isn't strongly-named?
A strongly-named assembly has:
- Assembly name.
- Version number.
- Culture information.
- Public/private key pair.
- Digital signature.
Example:
MyLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
A non-strongly-named assembly does not contain a cryptographic signature or public key token.
Strong naming provides:
- Unique assembly identity.
- Protection against assembly name collisions.
- Versioning support.
- Eligibility for installation in the Global Assembly Cache (GAC) on .NET Framework.
Important: Strong naming identifies the publisher and ensures assembly identity, but it is not a security boundary and should not be confused with code signing.
159. What is the JIT (Just-In-Time Compiler)?
The Just-In-Time (JIT) compiler is a component of the .NET runtime that converts Intermediate Language (IL) code into native machine code at runtime.
Compilation process:
C# Source Code
↓
C# Compiler
↓
IL (Intermediate Language)
↓
JIT Compiler
↓
Native Machine Code
When a method is called for the first time, the JIT compiler converts that method's IL into machine code specific to the current processor architecture.
Example:
public void Print()
{
Console.WriteLine("Hello");
}
The method is JIT-compiled the first time it executes.
Benefits of JIT
- Produces machine code optimized for the current CPU.
- Reduces application size because native code is not stored on disk.
- Supports platform independence through IL.
- Compiles only methods that are actually executed.
Limitations of JIT
- Application startup can be slower because methods must be compiled during execution.
- First invocation of a method incurs compilation overhead.
- CPU resources are consumed during runtime compilation.
160. What is NGEN (Native Image Generator)?
NGEN is a tool that precompiles IL assemblies into native machine code before the application runs.
Example command:
ngen install MyApplication.exe
This generates a native image and stores it in the Native Image Cache.
When the application starts, the CLR can use the precompiled native image instead of invoking the JIT compiler for many methods.
Compilation process:
C# Source Code
↓
C# Compiler
↓
IL Assembly
↓
NGEN
↓
Native Image Cache
↓
Application Execution
Benefits of NGEN
- Faster application startup.
- Reduces JIT compilation overhead.
- Lower CPU usage during application startup.
- Useful for large desktop applications and server applications with many assemblies.
Limitations of NGEN
- Generates larger disk footprint because both IL and native code are stored.
- Native images are platform-specific.
- Can become invalid when dependencies or runtime versions change.
- May not optimize as effectively as JIT for the current execution environment.
- Requires maintenance of the Native Image Cache.
JIT vs NGEN
| Feature | JIT | NGEN |
|---|---|---|
| Compilation Time | Runtime | Before Execution |
| Startup Performance | Slower | Faster |
| Disk Usage | Smaller | Larger |
| CPU Usage at Startup | Higher | Lower |
| Machine Specific | No | Yes |
| Runtime Optimization | Better | Limited |
Which is better?
Neither is universally better.
- JIT provides better runtime optimization because it has knowledge of the actual execution environment.
- NGEN improves startup performance by reducing or eliminating runtime compilation.
Historically:
- .NET Framework primarily used JIT and NGEN.
- Modern .NET (.NET Core, .NET 5+) uses JIT by default and introduces ReadyToRun (R2R) and Ahead-of-Time (AOT) compilation technologies as successors to NGEN.
In Summary:
JIT (Just-In-Time) compiles IL code into native machine code when methods are first executed, providing platform independence and runtime optimizations. NGEN (Native Image Generator) precompiles assemblies into native code before execution, reducing startup time and JIT overhead. JIT offers better runtime optimization and lower disk usage, while NGEN provides faster application startup at the cost of larger native images and reduced flexibility.
| Static vs Singleton :👈 | 👉:C# Sharp QA-17 |