Class Member
In our example, there are only two out of class members, they are field and methods. We'll learn about the rest next time.
To create field in class, we can simply use this syntax:
In previous example we could see:
If there's no field initializer in the declaration, compiler will set to default value of type. See the default value table.
When you want to declare same type multiple field with initializer, do as follow:
The minimum syntax for declaring a method includes the following components:
For example:
Fields
A field is a variable that belongs to a class.- It can be of any type, either predefined or user-defined.
-
Like all variables, fields store data and have the following characteristics:
- They can be written to.
- They can be read from.
To create field in class, we can simply use this syntax:
[Access Modifier:Optional] Type [Field Name]
In previous example we could see:
private uint accoundID; //field accountID private string accountFirstName; //field accountFirstName private string accountLastName; //field accountLastName private decimal accountBalance; //field accountBalance
Explicit and Implicit Field Initialization
A field initializer is part of the field declaration and consists of an equal sign followed by an expression that evaluates to a value. Example:int accountID = 20; //Initialize accountID to 20
If there's no field initializer in the declaration, compiler will set to default value of type. See the default value table.
int accountID; //Initialize accountID to 0
Multiple Field Declaration
When you have field with same type, you can declare them at once. For example you have accountFirstName and accountLastName with exactly same string type. You can declare them as follow:private string accountFirstName,accountLastName;
When you want to declare same type multiple field with initializer, do as follow:
private string accountFirstName = "Yang",accountLastName = "Sopiana";
Methods
A method is a named block of executable code that can be executed from many different parts of the program, and even from other programs. When a method is called, or invoked, it executes the method’s code, and then returns to the code that called it and continues executing the calling code. Some methods return a value to the position from which they were called.The minimum syntax for declaring a method includes the following components:
- Return type: This states the type of value the method returns. If a method doesn’t return a value, the return type is specified as void.
- Name: This is the name of the method.
- Parameter list: This consists of at least an empty set of matching parentheses.
- Method body: This consists of a matching set of curly braces containing the executable code.
[Access Modifier : Optional] [Return Type] Method's Name ([Parameter List : Optional])
{
[Method's Body]
}
public account(uint ID, string firstName, string LastName) //Class's Constructor { accoundID = ID; accountFirstName = firstName; accountLastName = LastName; } public void store(decimal ammount) //method store { Console.WriteLine(accountFirstName + " " + accountLastName + " store :" + ammount); accountBalance += ammount; } public void withdraw(decimal ammount) //method withdraw { Console.WriteLine(accountFirstName + " " + accountLastName + " withdraw :" + ammount); accountBalance -= ammount; } public void checkBalance() //method checkBalance { Console.WriteLine(accountFirstName + " " + accountLastName + " has " + accountBalance); }
0 comments:
Post a Comment