title: When and why to use a "ValueObject" base class (from the Microsoft Docs) in C#?
body: <p>I am trying to understand the use case for ValueObject in C#, when to use and what's the need for ValueObject. I see in the documentation that it can be used when we want to initialize object and then don't want to change the properties that mean making it immutable, but is it not same with Singleton pattern where you can initialize object properties in constructor and it will persist for the lifetime of the application ? Now why we need ValueObject and what is all with this EqualityComparer and hashCode() etc.</p>
  <p>Code from Microsoft Docs:</p>
  <pre><code>    public abstract class ValueObject
      {
          protected static bool EqualOperator(ValueObject left, ValueObject right)
          {
              if (ReferenceEquals(left, null) ^ ReferenceEquals(right, null))
              {
                  return false;
              }
              return ReferenceEquals(left, null) || left.Equals(right);
          }
      
          protected static bool NotEqualOperator(ValueObject left, ValueObject right)
          {
              return !(EqualOperator(left, right));
          }
      
          protected abstract IEnumerable&lt;object&gt; GetEqualityComponents();
      
          public override bool Equals(object obj)
          {
              if (obj == null || obj.GetType() != GetType())
              {
                  return false;
              }
      
              var other = (ValueObject)obj;
      
              return this.GetEqualityComponents().SequenceEqual(other.GetEqualityComponents());
          }
      
          public override int GetHashCode()
          {
              return GetEqualityComponents()
                  .Select(x =&gt; x != null ? x.GetHashCode() : 0)
                  .Aggregate((x, y) =&gt; x ^ y);
          }
          // Other utility methods
      }
  </code></pre>
  <p>Now Address entity:</p>
  <pre><code>    public class Address : ValueObject
      {
          public String Street { get; private set; }
          public String City { get; private set; }
          public String State { get; private set; }
          public String Country { get; private set; }
          public String ZipCode { get; private set; }
      
          public Address() { }
      
          public Address(string street, string city, string state, string country, string zipcode)
          {
              Street = street;
              City = city;
              State = state;
              Country = country;
              ZipCode = zipcode;
          }
      
          protected override IEnumerable&lt;object&gt; GetEqualityComponents()
          {
              // Using a yield return statement to return each element one at a time
              yield return Street;
              yield return City;
              yield return State;
              yield return Country;
              yield return ZipCode;
          }
      }
  </code></pre>
  <p>In above code what is this address and why did we use ValueObject and what is all this equalOperator and all written in ValueObject class, just trying to understand the use case of using ValueObject in real time and why do we need it and it's real time use case on what scenarios to use ValueObjects, and why do we need Equality operator, not equal operator inside valueObject.</p>
  <p>I am novice on this topic.</p>