Any class in an object-oriented language is the system and members of the property. These are where the real business logic or functionality is written and executed. This tutorial explains how to build and use methods and properties in C #.
C# Methods:
Method is the subject of any object oriented language. All C # programs are constructed of a number of classes and most classes contain methods. At the request of the class when an object is called. Object-oriented concepts of programming to say that the data members of each object to represent their state and methods represent the object's behavior.
Each method is declared as follows:
Return-type methodname ( Parameterslist );
For better understanding of methods let consider following example. We have a class Man. It can have many fields like that:
- public class Man
- {
- public Man(){}
- private int m_old;
- private string m_name;
- public string WhatIsYourName()
- {
- Console.WriteLine(m_name);
- return m_name;
- }
- public string HowOldAreYou()
- {
- Console.WriteLine(m_old.ToString());
- return m_old;
- }
- }
The private members m_old & m_name define some state of objects that can be created as instances of our class. Also the class Man has six methods, which serve a number of our requests. Technique string WhatIsYourName() writes current object?s name to the console & returns it, & the second three similar to first return age of man & also writes an output to the console.
The return type in the example above returns strings, which is an in-built data type. The methods can also return any generic C# type or any custom types created by us.
0 comments:
Post a Comment