Loading...

C# Overloading

Put two methods in one class that both use the name paint. One header is static void paint(string word). The other is static void paint(int n). The bodies are short: the first writes word, the second writes n. From Main call paint("ok"); and then paint(3);. The screen should show two lines: ok on the first line and 3 on the second. C# does not treat the second paint as a clash. It treats the two headers as different methods because the parameter lists differ. That shared name with different lists is what this page teaches. Keep the public class named Pine.

Same name, different lists

Until now each new idea used a fresh method name: ring, greet, twice. Here the name stays paint on purpose. What changes is what sits between the parentheses. One list holds a string. Another list holds an int. The compiler keeps both because those lists are not the same.
Open the Pine file. The class name stays Pine. This page does not rename the type. Delete any twice demo that returns n * 2 if that shape is still all you have in Main. Delete any greet leftover. Delete any ReadLine line. The file you keep has no wait for typing.
Outside Main, still inside the class braces, write both paint headers. Keep void on this page: each body writes, and nothing needs to come back to Main through return. The first body uses Console.WriteLine(word);. The second uses Console.WriteLine(n);. Save the file before you run. The full first program is in the next box.

The file you keep

This is the version you run first. Main holds two calls. The class holds two paint methods that share a name.

paint string and paint int

csharp
using System;

public class Pine
{
    public static void Main()
    {
        paint("ok");
        paint(3);
    }

    static void paint(string word)
    {
        Console.WriteLine(word);
    }

    static void paint(int n)
    {
        Console.WriteLine(n);
    }
}

Read it from the top. using System; prepares short names. public class Pine names the type. public static void Main() is where the runtime starts. The first call is paint("ok"). The argument is a string, so C# runs the body that takes string word and writes ok. The second call is paint(3). The argument is an int, so C# runs the body that takes int n and writes 3. The name paint is the same both times. The list inside the parentheses is what picks the body. Two lines on the screen: ok, then 3.

Run it with dotnet run. The program writes two lines and stops. It does not wait for you to type. If dotnet run waits, ReadLine is still in the file. Delete that line and keep the two paint calls plus the two paint methods above. The screen shows ok, then 3. If both lines show ok, you may have left only the string method and written a fixed word for every call. If both lines show 3, you may have left only the int method. Keep both headers. If the build reports that paint is already defined, check whether both headers have the same list — for example two copies of paint(string word). Overloading needs a real difference in the list, not a second copy of the same header with a different parameter name alone. You do not reinstall the .NET SDK for a screen that shows the wrong lines.

A second list with two words

Parameter count can differ too. Add a third overload that takes two string values and writes them on one line with a space. Leave the first two paint methods in place when you practice all three together. For a focused demo of this shape alone, you can use a file that holds only this list.

paint two strings

csharp
using System;

public class Pine
{
    public static void Main()
    {
        paint("hi", "sam");
    }

    static void paint(string a, string b)
    {
        Console.WriteLine(a + " " + b);
    }
}

This is the second file. It teaches count as a list difference. Run dotnet run. The screen should show one line: hi sam. The call has two arguments. The method has two parameters. That match is enough for C# to choose this body. If you still have the earlier paint(string word) and paint(int n) in the same class, you can keep them. A call with two string values will not pick the one-argument string method, because the counts differ.
When you put all three overloads in one file for practice, Main can call each shape on its own line. A call with one string still goes to paint(string word). A call with one int still goes to paint(int n). A call with two string values goes to paint(string a, string b). The shared name does not erase those distinctions. After you have seen hi sam, put the kept two-call file back if that is the shape you want to save: paint("ok"); then paint(3);.

Empty list beside a string list

A method with empty parentheses is also a different list. You can keep paint() with no parameters next to paint(string word). The empty call paint(); runs the empty-list body. The call paint("go"); runs the string body.

paint empty and paint string

csharp
using System;

public class Pine
{
    public static void Main()
    {
        paint();
        paint("go");
    }

    static void paint()
    {
        Console.WriteLine("ready");
    }

    static void paint(string word)
    {
        Console.WriteLine(word);
    }
}

This is the third file. Run dotnet run. The screen should show ready on the first line and go on the second. The first call has nothing between the parentheses, so it matches paint(). The second call sends a string, so it matches paint(string word). Renaming one method to paintReady would also build, but that is a different skill. This page keeps the name paint and lets the lists carry the difference.

Which call runs which header

Read the calls beside the headers. The argument list in the call must fit the parameter list in the header. Types and count both matter on this beginner shape.

📊 call beside matching paint

Call in Main Header that runs Screen line
paint("ok"); paint(string word) ok
paint(3); paint(int n) 3
paint("hi", "sam"); paint(string a, string b) hi sam
paint(); paint() ready

A common mix-up is writing two methods that both say paint(string word) and expecting the second one to count as an overload. Changing only the parameter name to paint(string label) does not create a new list for the compiler. The type and the count are what matter here. Another mix-up is calling paint(true) when you never wrote a bool parameter list. Then the build cannot find a matching paint. Add a header that takes the type you want, or change the call to a type you already support.
Writing Console.WriteLine("paint"); in Main writes the word paint. It does not call any method. Keep paint("ok"); when you want the overload that takes a string. Leaving a leftover twice method in the same file can confuse the screen if Main still writes twice(7). Clear Main down to the paint calls you are testing, and keep only the paint overloads you need for that run.
Do not nest a paint method inside Main’s braces. Methods sit inside the class, next to Main. Two overloads are two sibling methods in that same class body.

You can keep one method name and several parameter lists in the same class. C# picks the body from the call’s argument list. The screen shows the line from the matching body. Two headers with the same list are not overloads. The next page is scope, where you see which names are visible inside a method and which stay outside.

The screen holds the matching body

The file you keep writes ok, then 3, when Main calls paint("ok"); and paint(3);. The shared name paint is in each call, and the letters paint are not on the screen as a printed label unless you quoted them. Nesting a paint method inside Main is not the shape this lesson builds.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top