C# tutorial OOP-methods


Methods

A method is a member of a class that can be called to perform a specific task. When you define a method, you need to specify the access level (private, protected, or public), the return type, and the name of the method. The code of the method will be placed in curly braces.

Access_Level Return_Type Method_Name{

//code

}

The private keyword will be used if you want the mehtod can be accessed only in the class itself. To provide the method public access, you need to use the public keyword.

A method that does not return a value will have void type. If it designed to return a value, the return type of the method must be the same as the type of that value.

 

    class Student

    {

        private string stnumber;

        private string stname;

        private string stsex;

 

        public void setnumber()//setnumber method

        {

            Console.WriteLine("Enter student's number:");

            stnumber =Console.ReadLine();

        }

 

        public void getnumber()//getnumber method

        {

            Console.WriteLine("Student's number is:{0}",stnumber);

        }

 

 

    }



Comments

KrishnaMohan comment

 KrishnaMohan

Check this also for C# interview questions
http://skillgun.com/csharp/interview-questions-and-answers


2014-09-02
CAPTCHA image




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.