A class with method and member variables : Member Variable « Class « C# / CSharp Tutorial






using System;

class Employee
{
    // constructor
    public Employee(string name, float billingRate)
    {
        this.name = name;
        this.billingRate = billingRate;
    }
    // figure out the charge based on Employee's rate
    public float CalculateCharge(float hours)
    {
        return(hours * billingRate);
    }
    // return the name of this type
    public string TypeName()
    {
        return("Employee");
    }
    
    private string name;
    protected float billingRate;
}
class MainClass
{
    public static void Main()
    {
        Employee Employee = new Employee("A", 21.20F);
        Console.WriteLine("Name is: {0}", Employee.TypeName());
    }
}
Name is: Employee








7.5.Member Variable
7.5.1.fields
7.5.2.A class with method and member variables
7.5.3.field initialization
7.5.4.Add a method to access the field variables
7.5.5.Call base constructor to init member variables
7.5.6.Use this and base together to init a class
7.5.7.How to use a 'has a' relationship
7.5.8.Illustrates how to assign default values to fields using initializers