Article Tags
Article Tags
How to create a singleton in C#? (Design patterns)
Why static field private constructor is not a universal singleton? It can indeed prevent external new, but it cannot prevent reflection or serialization bypass. For example, using Activator.CreateInstance(typeof(Singleton), true) can still generate new instances - this will quietly destroy singleton semantics in unit testing or deserialization scenarios. You must add [Obsolete("UseInstanceinstead",error:true)] to the private constructor and cooperate with static analysis tools to intercept it in advance. If the class implements ISerializable, you must rewrite GetObjectDa
Apr 02, 2026 am 12:08 AM
How to use switch expressions in C#? (Pattern matching)
The fundamental difference is that the switch expression must have a return value and each branch is returned explicitly with =>, while the traditional switch statement is an executable code block without a return value.
Apr 02, 2026 am 12:04 AM
How to create a DLL in C#? (Class library project)
Creating a DLL in C# actually generates a .NET assembly: create a new ClassLibrary project and compile it, and the output is a .dll of IL bytecode (such as bin/Debug/net8.0/MyLib.dll), which can only be referenced by .NET projects and does not support direct LoadLibrary calls from C/C; COM, C/CLI or cross-process communication is required to achieve interoperability.
Apr 01, 2026 am 12:12 AM
How to use constructor injection in C#? (SOLID principles)
Why publicclassService{publicService(ILoggerlogger){...}} is not equivalent to "using constructor injection"? Constructor injection does not count as long as you write a constructor with parameters. It must be registered and parsed with the dependency injection container (such as Microsoft.Extensions.DependencyInjection), otherwise it will just pass the value of ordinary parameters. A common mistake is to manually create a new instance: newService(newConsoleLogger()) - this bypasses the DI container and the ILogger cannot be replaced or
Apr 01, 2026 am 12:11 AM
How to convert enum to string in C#? (Enum methods)
ToString() is the fastest but returns a name instead of a value and does not support Flags/Description metadata; Enum.GetName() requires an integer and returns null for illegal values; the description text should be cached to avoid reflection; JsonSerializer serializes to numeric values by default, and JsonStringEnumConverter needs to be explicitly configured.
Mar 31, 2026 am 12:10 AM
How to use records in C#? (Immutable data types)
Record in C# is an immutable type with value semantics. The core is to automatically implement Equals, GetHashCode, ToString and with expressions to support init/get-only attributes. Mixing sets will destroy semantic consistency. Recordstruct enforces value type semantics and has no inheritance. With is only a shallow copy. Deserialization requires explicit configuration of init attribute support.
Mar 31, 2026 am 12:09 AM
How to use extension methods in C#? (Static class guide)
The Extension method must be defined in a non-generic static class, and the this parameter must be the first parameter and the type cannot be dynamic; otherwise, the compilation error CS1106 will be reported or intelligent prompts will not be available.
Mar 30, 2026 am 12:06 AM
How to loop through a dictionary in C#? (Foreach key value)
In C#, you must use foreach(KeyValuePairkvpindict) to traverse Dictionary and access it through kvp.Key/kvp.Value; traversing key values directly will cause a compilation error. Keys/Values are read-only copies. The for loop needs to be converted to an array. LINQ delayed execution is still limited by enumerator modification.
Mar 30, 2026 am 12:04 AM
How to use dependency injection in C#? (DI patterns)
Why the IServiceCollectionAddXxx method must be called in Program.cs is because the .NET DI container freezes the registry when the application starts, and subsequent calls to AddScoped or AddSingleton will not take effect - instead of reporting an error, it is silently ignored. The registration code you wrote may not enter the container at all. Common error phenomenon: InvalidOperationException:Noservicefortype'IRepository'hasbeenregistered, but you clearly wrote services.AddSc
Mar 29, 2026 am 12:05 AM
How to remove duplicates from a list in C#? (Distinct method)
Distinct() defaults to deduplicating reference types by reference instead of value; it needs to implement IEquatable or IEqualityComparer, or use DistinctBy() (.NET6); it returns IEnumerable, delayed execution, and must be converted to a collection to be mutable.
Mar 29, 2026 am 12:04 AM
How to use string builder in C#? (StringBuilder optimization)
StringBuilder is faster than string splicing when the cumulative modification is ≥ 4-5 times, because its variable buffer avoids frequent creation of objects; if it is less than this number, string is better because StringBuilder has initialization and expansion overhead.
Mar 28, 2026 am 12:11 AM
How to initialize an array in C#? (Array syntax guide)
It is safest to initialize an array with new, especially when the length needs to be specified; new[] is only used for declaration and initialization, multi-dimensional arrays must use new; reference type elements default to null, and value types are cleared.
Mar 28, 2026 am 12:07 AM
How to sort a dictionary by value in C#? (OrderBy guide)
OrderBy sorts the dictionary by value and returns a new sequence instead of modifying the original dictionary; explicit assignment or traversal of results is required, value types must be comparable, comparison of null values and strings needs to be actively handled, and results should be cached when performance is sensitive.
Mar 27, 2026 am 12:09 AM
How to use try catch blocks in C#? (Exception handling)
The time to use try instead of throwing exceptions directly is when calling external resources (such as file reading and writing, network requests, database queries) or third-party methods that may fail; for pure memory calculations and parameter verification failures, guardclause or TryXxx() methods should be used first.
Mar 27, 2026 am 12:08 AM
Hot tools Tags
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
vc9-vc14 (32+64 bit) runtime library collection (link below)
Download the collection of runtime libraries required for phpStudy installation
VC9 32-bit
VC9 32-bit phpstudy integrated installation environment runtime library
PHP programmer toolbox full version
Programmer Toolbox v1.0 PHP Integrated Environment
VC11 32-bit
VC11 32-bit phpstudy integrated installation environment runtime library
SublimeText3 Chinese version
Chinese version, very easy to use
Hot Topics
20513
7
13626
4



