Author: Brandon Pearman

The views expressed here are mine alone and do not reflect the view of my employer.

Overloading is when there are two methods with the same name but different signatures. Overloaded methods are compile time Polymorphism, since the compiler is aware of which method is being called at compile time.

public class Calculator
{
  public void Add(int a, int b)
  {
      Console.WriteLine(a + b);
  }

  public void Add(int a, int b, int c)
  {
     Console.WriteLine(a + b + c);
  }

  public void Add(List{{'<'}}int{{'>'}} nums)
  {
      Console.WriteLine(nums.Sum());
  }
}

Since you have three methods with the same name, when you type out "Add(" then intellisense will popup with all the overloaded options.

Calculator calculator = new Calculator();
calculator.Add(1, 1); // 2
calculator.Add(1, 1, 1); // 3
calculator.Add(new List{{'<'}}int{{'>'}}() {1, 1, 1, 1 }); // 4

calculator.Add(...

operator overloading is when you tell operators (+ - * /) how to handle objects. For example:

public HobbyCollection AddHobbyCollections(HobbyCollection collection1, HobbyCollection collection2)
{
    HobbyCollection jointCollection = collection1 + collection2;
    return jointCollection;
}

+ does not normally know how to add two objects, therefore we need to overload the "+" operation in the class.

You can specify how two objects will interact with eachother, by using the "operator" keyword and then an operation such as:

public static HobbyCollection operator +(HobbyCollection collection1, HobbyCollection collection2)
{
    List< string> jointsItems = new List< string>();
    jointsItems.AddRange(collection1.Items);
    jointsItems.AddRange(collection2.Items);

    int jointValue = collection1.Value + collection2.Value;

    return new HobbyCollection()
    {
        Items = jointsItems,
        Value = jointValue
    };
}

A single class can have multiple overloads for an operator. Note: You can add any two objects/values together

public static HobbyCollection operator +(HobbyCollection collection1, int extraValue)
{
    collection1.Value += extraValue;

    return collection1;
}

Cast overloading is when you overload how a type is cast into another type. For example:

public Person EmployeeToPerson(Employee employee)
{
      Person person = (Person)employee;
      return person;
}

The classes Person and Employee dont know how to cast to and from one another. You can specify how two objects cast to each other with "implicit" or "explicit" casting "operators".

Explicit casting

Explicit casting means you have to cast it directly eg (Person)employee. In cases where information would be lost in the cast, explicit should be used to let the developer know information will be lost. for example:

int i = (int)1.23; // 1 (the .23 was lost)

After the "operator" keyword specify a type to cast to, and the Type it casts from will be an input into the method. Note: the method must be static and can implemented in the casting to or casting from type. eg below method can be put in Person or Employee class.

public static explicit operator Person(Employee employee)
{
    return new Person()
    {
        Name = employee.Name,
        Age = employee.Age
    };
}

This explicit casting can then be used like this:

public Person FireEmployee(Employee employee)
{
    Person person = (Person)employee;
    return person;
}

Implicit casting

With implicit casting you do not have to cast it directly. Use this when no information will be lost. for example:

double d = 2; // 2

After the "operator" keyword specify a type to cast to, and the Type it casts from will be an input into the method.

public static implicit operator Employee(Person person)
{
    return new Employee()
    {
        Name = person.Name,
        Age = person.Age
    };
}

This implicit casting can then be used like this:

public Employee EmployeePerson(Person person)
{
    Employee employee = person;
    return employee;
}

Note: You can cast objects into basic data types such as to an int.

public static explicit operator int(Person person)
{
      return person.Age;
}

Check out these links for more info:

My C# language repo