Object Class Methods : Member Method « Class « C# / CSharp Tutorial






using System;

public class Product {
    public string make;
    public string model;

    public Product(string make, string model) {
        this.make = make;
        this.model = model;
    }

    public void Display() {
        Console.WriteLine("make = " + make);
        Console.WriteLine("model = " + model);
    }

    public static Product Copy(Product p) {
        return (Product)p.MemberwiseClone();
    }
}
class MainClass {

    public static void Main() {
        Console.WriteLine("Creating Product objects");
        Product myProduct = new Product("Toyota", "MR2");
        Product myOtherProduct = new Product("Porsche", "Boxter");
        myProduct.Display();
        myOtherProduct.Display();

        Console.WriteLine("myProduct.ToString() = " + myProduct.ToString());
        Console.WriteLine("myProduct.GetType() = " + myProduct.GetType());
        Console.WriteLine("myProduct.GetHashCode() = " + myProduct.GetHashCode());
        Console.WriteLine("Product.Equals(myProduct, myOtherProduct) = " + Product.Equals(myProduct, myOtherProduct));
        Console.WriteLine("Product.ReferenceEquals(myProduct, myOtherProduct) = " + Product.ReferenceEquals(myProduct, myOtherProduct));

        myProduct = myOtherProduct;

        Console.WriteLine("Product.Equals(myProduct, myOtherProduct) = " + Product.Equals(myProduct, myOtherProduct));
        Console.WriteLine("Product.ReferenceEquals(myProduct, myOtherProduct) = " + Product.ReferenceEquals(myProduct, myOtherProduct));

        Product myOldProduct = Product.Copy(myProduct);
        myOldProduct.Display();

    }

}








7.4.Member Method
7.4.1.Define methods
7.4.2.Member Functions
7.4.3.Nested methods
7.4.4.Return a value from a member method
7.4.5.Define Rectangle class with method to compute the area
7.4.6.Object Class Methods