Object-oriented programming
Methods
A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments. In C#, every executed instruction is performed in the context of a method. The Main method is the entry point for every C# application and it is called by the common language run-time (CLR) when the program is started.
Method Signatures
Methods are declared in a class or struct by specifying the access level such as public or private, optional modifiers such as abstract or sealed, the return value, the name of the method, and any method parameters. These parts together are the signature of the method.Method parameters are enclosed in parentheses and are separated by commas. Empty parentheses indicate that the method requires no parameters.
The syntax of Methods:
<Access modifiers> <Return Type> <Method Name>(Parameter List)
{
Method Body
return result;
}
Access Modifier:
This determines the visibility of a variable or a method from another class.
Return type:
A method may return a value. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is void.
Method name:
Method name is a unique identifier and it is case sensitive. It cannot be same as any other identifier declared in the class.
Parameter list:
Enclosed between parentheses, the parameters are used to pass and receive data from a method. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters.
Method body:
This contains the set of instructions needed to complete the required activity.Return result:
Methods can return a value to the caller. If the return type, the type listed before the method name, is not void. If the return type is void, a return statement without a value is still useful to stop the execution of the method. Without the return keyword, the method will stop executing when it reaches the end of the code block.
namespace Age
{
class Program
{
public int CalcAge(string userInput)
{
#region age calculation
// Separate the date from DateTime
DateTime temptDate = DateTime.Now.ToLocalTime();
// Convert userInput from string to DateTime
DateTime userBirthdate = DateTime.Parse(userInput);
//subtract user Birthdate from Current Date in tempDays variables
TimeSpan tempDays = (temptDate - userBirthdate);
Console.WriteLine("Your Age in Days is {0}", tempDays);
// convert tempDays to int to calculate the age in years
int totalDays = tempDays.Days;
int yourAge = totalDays / 365;
Console.WriteLine("Your Age in years is {0}", yourAge);
#endregion
return yourAge;
}
static void Main(string[] args)
{
Console.WriteLine(DateTime.Now);
Console.WriteLine("Current date is " + DateTime.Now.ToString("dd/MM/yyyy \n"));
Console.WriteLine("Plz Enter Your Birthdate as: MM / DD/ YYYY");
// read the user birthdate & save it in userInput variable
string userInput = Console.ReadLine();
//make a copy from the class program "instance of a class"
Program p = new Program();
// Calling the method CalcAge & passing the userInput variable to it
p.CalcAge(userInput);
}
}
}
Post a Comment