Tag: csharp
All the articles with the tag "csharp".
Two simple tips for working with LINQ and IEnumerable<T>
Published:3 min readThis article provides two tips for working with LINQ and IEnumerable<T>: use Any() instead of Count() to check for non-empty return values, and use Enumerable.Empty<T> and never return null.
Getting the Windows Product ID using WMI
Published:2 min readThe Windows Product ID can be retrieved using WMI via the Win32_OperatingSystem class and the SerialNumber property. The relevant documentation can be found on MSDN and an example of code to query the property value is given. It has been tested successfully on various versions of Windows.
CodeDomProvider and Compiler Warning Levels
Published:3 min readBy default, the CodeDomProvider does not return warning messages in the CompilerResults that are returned when compiling code in Visual Studio. The default warning level is 4, and you can use CompilerParameters.WarningLevel to set the warning level and CompilerParameters.TreatWarningsAsErrors to abort the compilation for warnings.
Attaching the Debugger only in Debug
Published:2 min readThis article discusses how to use the ConditionalAttribute to create a helper class that includes methods that should only be called when the DEBUG symbol is defined. This way, when you compile with the RELEASE symbol defined all calls to the methods will simply be excluded from the generated MSIL.
Compute any hash for any object in C#
Published:6 min readThis article explains how to create an extension method in C# that can be used to calculate the hash of any object using various hashing algorithms such as MD5 or SHA-1. The extension methods use the DataContractSerializer to turn the object into a byte array which is then passed into the Cryptographic Service Provider to compute the hash. The article also covers the two types of hashing functions and their implementations.