← Back to C# Course | Chapter 5: Methods | Lesson 2 of 6

Parameters

Parameters are the blanks a method asks you to fill in, like telling a pizza place your size and toppings. The method uses your answers to do its job.

In this page:

  1. Parameters
Syntax
csharp
static void MethodName(type parameter1, type parameter2)
{
    // body
}

MethodName(argument1, argument2);

Parameters

Parameters let a method receive input values from its caller, each with a declared type and name. Arguments are passed by value for value types by default, meaning the method gets its own copy.

Multiple parameters are separated by commas in both the declaration and the call.

Note: Parameter names should describe their purpose, not just their type.

Example: Parameters

markup
using System;

class Program
{
    static void Greet(string name, int times)
    {
        for (int i = 0; i < times; i++)
        {
            Console.WriteLine($"Hello, {name}!");
        }
    }

    static void Main(string[] args)
    {
        Greet("Sam", 3);
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Passing arguments in the wrong order when the types are the same, which compiles but gives wrong results.
  2. Expecting a method to change the caller's int, when value-type parameters are copies.
  3. Passing too few or too many arguments, which is a compile error.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.