An Introduction to Structs in C#

A struct is a value type that is often used to house a group of related variables, such as the points of a rectangle, together. Structs share many characteristics with classes but they are, at the same time, very different. In this post, we will explore those commonalities and differences and explore some use cases for a struct. What is …

Constructor Chaining in C#

In this post, we will look at constructor chaining in C#. What is Constructor Chaining? Constructor chaining occurs when you use one constructor in a class to call another constructor on the same class. Often, because the constructors must have different signatures, you supply a safe or default value for the second constructor. Listing 1.1 – Constructor Chaining public MessagePrinter(string …

Using the yield keyword in C#

The yield contextual keyword was first introduced in C# 2.0 and despite that fact, it is rarely used. Perhaps it is not a well-known feature or perhaps it is misunderstood but it has plenty of uses. In this post, I will demonstrate how to use the yield contextual keyword and discuss some common scenarios where it would be useful. What does …

Working With Optional Arguments in C#

Optional arguments were introduced in C# 4. They are valid for constructors, indexers, methods, and delegates. In this post, I will cover the basic information needed to use them correctly. What are Optional Arguments? Optional arguments are exactly what they sound like: arguments that you are not required to supply because they have a default value already specified. If no …