How amazing C#/.Net is? Part 2

These “cool” (but less well-known) things help make the code faster; it is easier to read and understand.

Photo by Shahadat Rahman on Unsplash

Welcome back to part 2 of the Amazing C# article. You can review part 1 here.

1. Optional Parameters

Many of you will not know what optional parameters are (I know it from C ++, but recently I know that C # also has =)). Assuming there are no optional parameters, we want to run the Add functions as follows:

We need to overwrite the Add function overload 4 times.

Extremely annoying and time-consuming, right? With optional parameters, we can write a function that takes all of the parameters and specify one of the parameters as optional. Otherwise, it will use the default value:

If you have learned C ++, it does not matter with this concept. As you can see, it saves us a lot of coding time.

2. Anonymous Type

As mentioned in Part 1, Anonymous Type is beneficial in many cases. Suppose we have 1 Student class with 10 properties.

In case we want to get 2 fields, Name and Age of Student, to serialize to JSON for brevity, we write the following code.

The received Anonymous object will be an object with 2 properties: Name and Age. This object also implements the Equals () function so that we can compare these 2 objects easily. However, we also need to be careful with some cases of “equal but not equal.

3. Tuple

A tuple is a rather “strange” concept; I first met this concept in Python, now I know it in C #. It can be understood that a tuple is a “clump” of many fields, similar to a class, but define faster and easier. For example:

Anonymous type has one drawback; that is, it is impossible to use the anonymous type as the return type or to pass parameters to a function.

but with Tuple:

You may ask: Why not create a new class? Why do you use an anonymous type with tuple? Please answer:

  • There are some cases where we need to extract some fields; if we take a few fields we create a new class, the code will be quite redundant and meaningless.
  • If we create a new class, we have to override Equals and GetHashCode if we need to compare. Anonymous types and tuples have automatically implemented this function for us.

4. String and little-known things

Surely all of us have used strings to draw headers, footers like this?

The code we used was:

Similarly, when we have a list of strings, we want to add up all the list strings.

6. Initialize Collection (ToArray, ToList, ToDictionary, …)

When using Linq, after using Where, Select, … the return result is usually 1 IEnumerable, we can turn them into List or Array with available functions. ToList is the most used function:

The article is the end. Because the article content is limited, in some parts, I only introduce the overview, not go into detail.

Parts like Linq, Anonymous Type, Delegate, … you could find on my personal page. .”

Always be nice to anybody who has access to my toothbrush.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store