Mono BLOG

Blog about Microsoft technologies (.NET, ASP.NET Core, Blazor, EF Core, WPF, TypeScript, etc.)

Why Doesn't IEnumerable<T> Have a ForEach Method?

Allen Liu

In C#, types like List<T> (and others such as ImmutableList<T>) include a convenient ForEach method. It can be seen as a more functional-style alternative to the traditional foreach loop. For example:

var list = new List<int> { 1, 2, 3, 4, 5 };

// Traditional foreach loop
foreach (var n in list)
{
    Console.WriteLine(n);
}

// The ForEach method achieves the same result
list.ForEach(n => Console.WriteLine(n));

Additionally, the Array class provides a static ForEach method that serves a similar purpose. However, you'll quickly notice that this handy method is not...

Read full article

A Tour of Hash Functions: From MD5 to Argon2 and Beyond

Allen Liu

In a previous article, we explored the topic of salted password hashing. This time, we'll expand on that by taking a broader look at the world of hash functions.

Hash functions are crucial tools in computer science and cryptography, essential for tasks like data integrity verification, digital signatures, and secure password storage. There is a wide variety of hash functions, some common and others less so. This article will introduce several of them, highlighting their characteristics and primary use cases.

Early Hash Functions

When we talk about early hash functions, MD5 and **SHA...

Read full article

Why You Must Salt and Hash Passwords: A Journey to Secure Storage

Allen Liu

The topic of salting and hashing passwords is a fundamental concept in security, but why is it so crucial? This article will guide you through the evolution of password storage, from insecure to secure, to demonstrate the necessity of salted hashing and how to implement it.

We've all heard that passwords should never be stored in plaintext. Instead, they should be hashed, and more specifically, salted and hashed. What is the purpose and necessity of this process? And how can we achieve it in C#? Let's explore these questions by progressing from the least secure to the most secure methods.

##...

Read full article

Advanced XML Parsing: A Performance Duel Between LINQ, XPath, and Regex

Allen Liu

Continuing our exploration from last time, let's dive deeper into the surprising performance differences in XML parsing. Our sample XML remains the same, provided by W3Schools:

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
    <calories>650</calories>
  </food>
  <!-- Three other <food> elements omitted for brevity -->
  <food>
    <name>Homestyle Breakfast</name>
    <price>$6.95</price>
    <description>Two eggs,...
Read full article

Fields vs. Properties: Why Not Just Use Public Fields in C#?

Allen Liu

When writing C# code, we frequently define auto-properties like this:

public class Person
{
    public int Age { get; set; }
}

This often raises a question, especially for developers new to the language: Why go through the trouble of writing an auto-property? Why not just declare a simple public field? Like this:

public class Person
{
    public int Age;
}

It's a valid question, and there are several important reasons behind this convention. Let's explore them.

A Fundamental Difference in Perspective

First and foremost, the way we perceive properties and fi...

Read full article

Efficient XML Parsing in C#: A Performance Comparison

Allen Liu

I recently encountered a task at work involving reading and writing XML, which led me to explore the performance implications of different approaches. I'd like to share my findings with you.

For our demonstration, we'll use a sample XML text from W3Schools:

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
    <calories>650</calories>
  </food>
  <!-- ... three <food> elements omitted for brevity ... -->
  <food>
   ...
Read full article