Complete C# Tutorial

C# Required Tutorial – Required Example with Real-World Scenario

Imagine you’re filling out a passport application form 🛂. You enter your name, birth date, and nationality. But when you try to submit it without adding a passport-size photo 📸, the system won’t let you! Why? Because that field is required!

That’s exactly what the required keyword in C# does! It forces you to provide values for certain properties before an object is used. No more missing important details—C# ensures your object has all necessary information before running! 😃

What is the Required Keyword in C#?

The required keyword in C# was introduced in C# 11. It ensures that certain properties must be initialized when an object is created.

Before required, you could create an object without setting important properties, which could lead to bugs and unexpected behavior. But now, with required, C# makes sure you don’t forget them!

Real-World Example: A Flight Ticket Booking System ✈️

Let’s say you’re booking a flight ticket online. Some details, like:

  1. Passenger Name
  2. Passport Number
  3. Flight Date

are mandatory. Without them, you can’t complete your booking!

Similarly, in C#, we can force certain properties to be filled when an object is created.

Required Example C# – Complete Code

Let’s create a FlightBooking system where some properties must be set before using an object.

Step 1: Create the FlightBooking Class

				
					class FlightBooking
{
    public required string PassengerName { get; set; }
    public required string PassportNumber { get; set; }
    public DateTime FlightDate { get; set; }
}
				
			

Here, PassengerName and PassportNumber must be provided before an object is created.

Step 2: Try Creating an Object Without Required Properties

				
					class Program
{
    static void Main()
    {
        FlightBooking booking = new FlightBooking();
    }
}
				
			

Output:

				
					Error: Required member 'PassengerName' must be set.
Error: Required member 'PassportNumber' must be set.
				
			

Oops! 🚨 C# won’t let us create an object without these properties!

Step 3: Create an Object Correctly (With Required Properties)

				
					class Program
{
    static void Main()
    {
        FlightBooking booking = new FlightBooking
        {
            PassengerName = "Steven Johnson",
            PassportNumber = "AB123456",
            FlightDate = new DateTime(2025, 5, 10)
        };

        Console.WriteLine($"Booking Confirmed for {booking.PassengerName} on {booking.FlightDate.ToShortDateString()}");
    }
}
				
			

Output:

				
					Booking Confirmed for Steven Johnson on 10-05-2025
				
			

Now, it works perfectly! 🎉

How This Works?

✔️ The required keyword ensures that PassengerName and PassportNumber must be set.
✔️ When we forgot to initialize them, C# gave an error.
✔️ Once we provided values, the object was created without any issues.

This is super helpful when working with important data that should never be left empty!

When Should You Use Required?

🟢 Use required when:

✅ You have important properties that must always have a value.
✅ You want to avoid missing data errors.
✅ You are designing strict data models where every object must have necessary details.

🔴 Don’t use required when:

❌ Some properties can be optional.
❌ You allow default values or lazy initialization.

Conclusion

So, what did we learn today? The required keyword makes sure that critical properties always have values before an object is used. This helps prevent missing data issues, reduce errors, and improve code reliability. 🚀

Next What?

In the next lesson, you’ll learn about Iterators in C#! Ever wondered how foreach works behind the scenes? Or how you can iterate over collections in a smart way? That’s exactly what we’ll explore next! Stay tuned and happy coding! 💡🔥

Leave a Comment

Share this Doc

Required

Or copy link