the overridden methods of the System.Object class : Class Deriving « Class Interface « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Class Interface » Class DerivingScreenshots 
the overridden methods of the System.Object class
 

using System;
using System.Collections;


public class Starter {
    public static void Main() {
        Employee obj1 = new Employee(5678);
        Employee obj2 = new Employee(5678);
        if (obj1 == obj2) {
            Console.WriteLine("equals");
        else {
            Console.WriteLine("not equals");
        }
    }
}

class Employee {

    public Employee(int id) {
        if ((id < 1000|| (id > 9999)) {
            throw new Exception(
                "Invalid Employee ID");
        }

        propID = id;
    }

    public static bool operator ==(Employee obj1, Employee obj2) {
        return obj1.Equals(obj2);
    }

    public static bool operator !=(Employee obj1, Employee obj2) {
        return !obj1.Equals(obj2);

    }

    public override bool Equals(object obj) {
        Employee _obj = obj as Employee;

        if (obj == null) {
            return false;
        }
        return this.GetHashCode() == _obj.GetHashCode();
    }

    public override int GetHashCode() {
        return EmplID;
    }

    public string FullName {
        get {
            return propFirst + " " +
                propLast;
        }
    }

    private string propFirst;
    public string First {
        get {
            return propFirst;
        }
        set {
            propFirst = value;
        }
    }

    private string propLast;
    public string Last {
        get {
            return propLast;
        }
        set {
            propLast = value;
        }
    }

    private readonly int propID;
    public int EmplID {
        get {
            return propID;
        }
    }

    public override string ToString() {
        return FullName;
    }
}

 
Related examples in the same category
1.Demonstrates deriving a new class from a base class in another assemblyDemonstrates deriving a new class from a base class in another assembly
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.