0
0
C Sharp (C#)programming~15 mins

Value equality in records in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Value equality in records
📖 Scenario: Imagine you are creating a simple contact list app. You want to store contacts as records and check if two contacts are the same based on their values.
🎯 Goal: Build a C# program that creates two contact records with the same data and checks if they are equal using value equality.
📋 What You'll Learn
Create a record named Contact with two properties: string Name and string Phone.
Create two Contact objects with the same Name and Phone values.
Compare the two Contact objects using the == operator.
Print "Equal" if they are equal, otherwise print "Not Equal".
💡 Why This Matters
🌍 Real World
Records are useful for storing data where you want to compare objects by their content, like contacts, products, or settings.
💼 Career
Understanding value equality in records helps in writing clean, bug-free code for data models in business applications.
Progress0 / 4 steps
1
Create the Contact record
Create a record called Contact with two properties: string Name and string Phone.
C Sharp (C#)
Need a hint?

Use the record keyword to define a record with positional parameters.

2
Create two Contact objects with the same values
Create two Contact objects named contact1 and contact2 with Name set to "Alice" and Phone set to "12345".
C Sharp (C#)
Need a hint?

Use the new keyword to create objects from the Contact record.

3
Compare the two Contact objects using value equality
Use an if statement to compare contact1 and contact2 using the == operator.
C Sharp (C#)
Need a hint?

Records in C# support value equality with the == operator by default.

4
Print the comparison result
Inside the if statement, print "Equal". Inside the else, print "Not Equal".
C Sharp (C#)
Need a hint?

Use Console.WriteLine to print the result.