C#: Passing an argument or a variable into a method

This entry is part 16 of 71 in the series C#

As in any other programming languages, we can pass variables or arguments into a function or method.

using System;

namespace Hello
    class Friend
        private void displayName(String name)
            Console.WriteLine("Hello, what is your name?");
            Console.WriteLine("Your name is " + name);

        static void Main()
            string name = "John";

            Friend friend = new Friend();           

The displayName() has one string argument.

The argument is initialized in the Main() body and printed out by the member function displayName().

I have declared the displayName() method as private.  Its use will be restricted to member functions of the same class.  And in this example, the member functions of Friend class.

In our later examples, we will use constructor to initialize a variable or argument.



Series Navigation<< C#: Pascal and camel case format
C#: Access modifiers for classes >>