Skip to main content

Posts

Showing posts from January, 2022

Record in C#

  Record in C# Introduction With the release of C# 9, Microsoft introduced records. The record keyword gives a reference type new superpowers like immutability declared with positional records (or by using init-only properties), equality comparisons that mimic value types, and with-expressions that allows you to create a new record instance with the same property values, the properties you need to change. This drastically simplifies the process to copy objects.   The release of C# 10 brings records structs. They’ll carry a lot of the advantages of C# 9 records (which are reference types, like classes), but there are differences with structs because, structs are different from classes! In this article, we will see what a record struct is, and why a record class doesn’t behave like a record struct. Syntax First of all, Microsoft has made an improvement to record classes. With C# 9, to declare a record you replaced the “class” keyword with “record.” To avoid confusion...

Yield Return in C# Made easy

  Yield Return in C# Made easy The CSharpNaija bring yet another important keyword in C# that is, yield return type. In the article below we will learn some programming techniques that revolve around C# yield return keywords. What MSDN has about Yield "The yield keyword signals to the compiler that the method in which it appears is an iterator block. The compiler generates a class to implement the behavior that is expressed in the iterator block. In the iterator block, the yield keyword is used together with the return keyword to provide a value to the enumerator object. This is the value that is returned, for example, in each loop of a foreach statement. The yield keyword is also used with break to signal the end of iteration." Operation of yield When combined with the return mechanism allowing creation, the function returns a value that handles certain information in the loop, and puts an end to certain conditions before it. This is contrary to the case of a norm...