Method Parameter

In this chapter you will learn:

  1. What is method parameter
  2. Pass int value to a function
  3. Parameters are local

What is method parameter

The parameters define the input value for a method.

The following code defines a method called add, which adds two int type variables together.

add method has two parameters, a and b. When calling the add method we must provide data for a and b.

using System;/* j a va 2s.  co m*/

class Program
{
    static int add(int a, int b)
    {
        int result = a + b;
        Console.WriteLine(result);
        return result;
    }

    static void Main(string[] args)
    {
        add(1, 2);
    }
}

Pass int value to a function

using System;/* ja  v  a  2 s . c o m*/

class MainClass
{
   public static void Main() {
      int SomeInt = 6;
      
      int s = Sum(5, SomeInt);
      
      Console.WriteLine(s);        
   }
   public static int Sum(int x, int y)               // Declare the method.
   {
      return x + y;                                  // Return the sum.
   }
   
}

The code above generates the following result.

Parameters are local

using System;//from  j  av  a  2s .  c o  m


class Program
{
    static void change(int i)
    {
        i = i + 1;
        Console.WriteLine("in change method:" + i);
    }


    static void Main(string[] args)
    {
        int i = 2;

        change(i);

        Console.WriteLine("in main method:" + i);
    }
}

The output:

From the output we can see that changing the value in change method has no effect on the variable i in main method.

For reference type parameters the value of the reference is copied not the object itself.

The following code defines a class Rectangle. Class is a reference type. The change method accepts the Rectangle type as its parameter. In the change method new value is assigned to the parameter.

using System;/* j  a v  a  2s.com*/

class Rectangle
{
    public int Width = 5;
    public int Height = 5;

}

class Program
{
    static void change(Rectangle r)
    {
        r.Width = 10;
    }
    static void Main(string[] args)
    {
        Rectangle r = new Rectangle();
        Console.WriteLine(r.Width);
        change(r);
        Console.WriteLine(r.Width);
    }
}

The output:

We can see that the main method gets the changed value from change method. The change method changes the value from the reference.

To see the effect of a copied reference we can change the code above as follows.

using System;/*  j  a  v  a 2 s . c o  m*/

class Rectangle
{
    public int Width = 5;
    public int Height = 5;

}

class Program
{
    static void change(Rectangle r)
    {
        r = null;
    }

    static void Main(string[] args)
    {
        Rectangle r = new Rectangle();
        Console.WriteLine(r.Width);
        change(r);
        Console.WriteLine(r.Width);
    }
}

The output:

Even if we set the reference in change method to null, the r in the main method still points the rectangle object.

Next chapter...

What you will learn in the next chapter:

  1. How Arguments Are Passed
  2. Parameter modifiers
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances