C#: Accessing methods using dot

Accessing a member function or method using dot is a simple and yet important concept in object-oriented programming.

In traditional procedural programming, we would access the function directly.  Now we create an instance of a class called output and use it to access the method displayname in the class.

Program output = new Program();

Those member functions in a class are call methods in OOP.

using System;

namespace Hello
{
    class Program
    {
        public void displayname()
        {
            string name;

            Console.WriteLine("Hello, what is your name?");
            name = Console.ReadLine();
            Console.WriteLine("Your name is " + name);
            Console.ReadLine();
        }

        static void Main()
        {
            Program output = new Program();
            output.displayname();
        }
    }
}

In the example above, I have created a method call displayname and I put that as a public function since at this moment I am not too concern about protecting it from being accessed.

Using the instance called output, we access the method using:

output.displayname();

hello

Series Navigation<< C#: The dot operator
C#: Pascal and camel case format >>