EmployeeDetail a,b; .... EmployeeCollection collection = a+b;
[Serializable]
public class EmployeeDetail
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { set;get; }
public override string ToString()
{
return string.Format("{0}{1}{2}{3}{4}", FirstName, string.IsNullOrWhiteSpace(MiddleName) ? null : "."
, MiddleName
, string.IsNullOrWhiteSpace(LastName) ? null : ".",
LastName).Trim();
}
}
public static EmployeeCollection operator +(EmployeeDetail a, EmployeeDetail b)
{
return new EmployeeCollection() { a, b };
}
EmployeeCollection collection = new EmployeeDetail(){FirstName="Jackson",LastName="Bruce"} + new EmployeeDetail(){FirstName="Michael",LastName="Jackson"} ;
EmployeeCollection collection = a + b + c;
public static EmployeeCollection operator +(EmployeeCollection collection, EmployeeDetail a)
{
collection.Add(a);
return collection;
}
public static EmployeeCollection operator +(EmployeeDetail a, EmployeeCollection collection)
{
return collection + a;
}
/// <summary>
/// 隐式类型转换
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static implicit operator EmployeeDetail(string name)
{
/// 其实在这里可以写一个正则表达式检查name的字符串格式是否合法,如果不合法就抛出异常
///
string[] arr;
return string.IsNullOrWhiteSpace(name) ? null :
new EmployeeDetail()
{
FirstName = (arr = name.Trim().Split('.'))[0]
,
LastName = arr.Length > 1 ? arr[arr.Length > 2 ? 2 : 1] : null,
MiddleName = arr.Length > 2 ? arr[1] : null
};
}
public static EmployeeCollection operator +(EmployeeDetail a, string b)
{
return new EmployeeCollection() { a, b };
}
static void Main(string[] args)
{
EmployeeDetail employee = "Jackson.Bruce";
Console.WriteLine("FirstName={0},MiddleNam={1},LastName={2}", employee.FirstName, employee.MiddleName, employee.LastName);
Console.WriteLine("toString={0}", employee);
Console.WriteLine();
EmployeeCollection collection = "Michael.Jackson" + employee;
collection += "Bruce.Lee";
foreach (var e in collection)
{
Console.WriteLine(e);
}
Console.WriteLine();
collection -= employee;
foreach (var e in collection)
{
Console.WriteLine(e);
}
Console.WriteLine("===end===");
Console.Read();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 重载运算符
{
[Serializable]
public class EmployeeDetail
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { set;get; }
public static EmployeeCollection operator +(EmployeeDetail a, EmployeeDetail b)
{
return new EmployeeCollection() { a, b };
}
public static EmployeeCollection operator +(EmployeeCollection collection, EmployeeDetail a)
{
collection.Add(a);
return collection;
}
public static EmployeeCollection operator +(EmployeeDetail a, EmployeeCollection collection)
{
return collection + a;
}
/// <summary>
/// 隐式类型转换
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static implicit operator EmployeeDetail(string name)
{
string[] arr;
return string.IsNullOrWhiteSpace(name) ? null :
new EmployeeDetail()
{
FirstName = (arr = name.Trim().Split('.'))[0]
,
LastName = arr.Length > 1 ? arr[arr.Length > 2 ? 2 : 1] : null,
MiddleName = arr.Length > 2 ? arr[1] : null
};
}
public static EmployeeCollection operator +(EmployeeDetail a, string b)
{
return new EmployeeCollection() { a, b };
}
public override string ToString()
{
return string.Format("{0}{1}{2}{3}{4}", FirstName, string.IsNullOrWhiteSpace(MiddleName) ? null : "."
, MiddleName
, string.IsNullOrWhiteSpace(LastName) ? null : ".",
LastName).Trim();
}
}
public class EmployeeCollection : List<EmployeeDetail>
{
public static EmployeeCollection operator +(EmployeeCollection a, string b)
{
a.Add(b);
return a;
}
public static EmployeeCollection operator +(string b, EmployeeCollection a)
{
return a + b;
}
public static EmployeeCollection operator -(EmployeeCollection a, EmployeeDetail b)
{
a.Remove(b);
return a;
}
}
class Program
{
static void Main(string[] args)
{
EmployeeDetail employee = "Jackson.Bruce";
Console.WriteLine("FirstName={0},MiddleNam={1},LastName={2}", employee.FirstName, employee.MiddleName, employee.LastName);
Console.WriteLine("toString={0}", employee);
Console.WriteLine();
EmployeeCollection collection = "Michael.Jackson" + employee;
collection += "Bruce.Lee";
foreach (var e in collection)
{
Console.WriteLine(e);
}
Console.WriteLine();
collection -= employee;
foreach (var e in collection)
{
Console.WriteLine(e);
}
Console.WriteLine("===end===");
Console.Read();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有