C# Tip: Required Members

Let’s talk about Required Members, introduced in C# 11, which allow you to declare properties that must be initialized, ensuring that objects are always created in a valid state. See the example in the code below.

public class Product
{
public r…


This content originally appeared on DEV Community and was authored by Juarez Júnior

Let’s talk about Required Members, introduced in C# 11, which allow you to declare properties that must be initialized, ensuring that objects are always created in a valid state. See the example in the code below.

public class Product
{
    public required string Name { get; set; }
    public required decimal Price { get; set; }
}

public class Program
{
    public static void Main()
    {
        Product product = new Product
        {
            Name = "Pen",
            // An exception will be thrown when compiling
            //Price = 2.99m
        };

        Console.WriteLine($"Product: {product.Name}, Price: {product.Price}");
    }
}

Explanation:

Required Members allow you to mark certain properties of a class as mandatory, forcing them to be initialized when the object is created. This is very useful to prevent objects from being created without all necessary information, which can lead to inconsistencies or runtime errors.

In the example above, we have a Product class with Name and Price properties, both of which are required. If a Product object is instantiated without initializing these properties, the compiler will raise an error, ensuring that the object always contains complete data from the start.

Source code: GitHub

I hope this tip helps you use Required Members to ensure the integrity of the objects created in your projects! Until next time.


This content originally appeared on DEV Community and was authored by Juarez Júnior


Print Share Comment Cite Upload Translate Updates
APA

Juarez Júnior | Sciencx (2024-09-15T12:00:00+00:00) C# Tip: Required Members. Retrieved from https://www.scien.cx/2024/09/15/c-tip-required-members/

MLA
" » C# Tip: Required Members." Juarez Júnior | Sciencx - Sunday September 15, 2024, https://www.scien.cx/2024/09/15/c-tip-required-members/
HARVARD
Juarez Júnior | Sciencx Sunday September 15, 2024 » C# Tip: Required Members., viewed ,<https://www.scien.cx/2024/09/15/c-tip-required-members/>
VANCOUVER
Juarez Júnior | Sciencx - » C# Tip: Required Members. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/15/c-tip-required-members/
CHICAGO
" » C# Tip: Required Members." Juarez Júnior | Sciencx - Accessed . https://www.scien.cx/2024/09/15/c-tip-required-members/
IEEE
" » C# Tip: Required Members." Juarez Júnior | Sciencx [Online]. Available: https://www.scien.cx/2024/09/15/c-tip-required-members/. [Accessed: ]
rf:citation
» C# Tip: Required Members | Juarez Júnior | Sciencx | https://www.scien.cx/2024/09/15/c-tip-required-members/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.