employeeid.cs
来自「C#高级编程第6版随书源代码 值得下载」· CS 代码 · 共 56 行
CS
56 行
using System;
using System.Collections.Generic;
using System.Text;
namespace Wrox.ProCSharp.Collections
{
[Serializable]
public struct EmployeeId : IEquatable<EmployeeId>
{
private readonly char prefix;
private readonly int number;
public EmployeeId(string id)
{
if (id == null) throw new ArgumentNullException("id");
prefix = (id.ToUpper())[0];
int numLength = id.Length - 1;
number = int.Parse(id.Substring(1, numLength > 6 ? 6 : numLength));
}
public override string ToString()
{
return prefix.ToString() + string.Format("{0,6:000000}", number);
}
public override int GetHashCode()
{
return (number ^ number << 16) * 0x15051505;
}
public bool Equals(EmployeeId other)
{
return (prefix == other.prefix && number == other.number);
}
public override bool Equals(object obj)
{
if (!(obj is EmployeeId)) return false;
return Equals((EmployeeId)obj);
}
public static bool operator ==(EmployeeId emp1, EmployeeId emp2)
{
return emp1.Equals(emp2);
}
public static bool operator !=(EmployeeId emp1, EmployeeId emp2)
{
return !emp1.Equals(emp2);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?