更新时间:2022-12-12 13:14:32
注意:这段代码编写得很快,而且,当它编译时,我还没有彻底测试过。
首先,如果你的代码正在创建'Person的实例列表,你想要执行一些筛选规则姓名,这是一个例子:
Note: this code was written quickly, and, while it compiles, I have not tested it thoroughly.
First, if your code is creating the list of instances of 'Person and you want to enforce some screening rule about names, here's an example:
public class Person
{
private static List<string> surnamesInUse = new List<string>();
private static List<string> firstNamesInUse = new List<string>();
private string lname;
private string lsurname;
public Person(string name, string surname)
{
if (name == String.Empty || surname == String.Empty)
{
throw new ArgumentException(
name和surname不能为空字符串);
}
lname = name.ToLower();
lsurname = surname.ToLower();
if(lname == lsurname)
{
抛出新的ArgumentException(
"name and surname cannot be empty strings"); } lname = name.ToLower(); lsurname = surname.ToLower(); if (lname == lsurname) { throw new ArgumentException(
名称和姓氏不能独立于案例) ;
}
if(firstNamesInUse.Contains(lname))
{
抛出新的DuplicateNameException(
"name and surname cannot match independent of case"); } if (firstNamesInUse.Contains(lname)) { throw new DuplicateNameException(