帮我看看C#属性Birthday set中的if 怎么会横为真

来源:百度知道 编辑:UC知道 时间:2024/06/11 20:38:54
using System;
using System.Collections.Generic;
using System.Text;

namespace 属性
{
class Program
{
static void Main(string[] args)
{
user u = new user();
u.Name = "张三";
u.Sex = "男";
u.Birthday = Convert.ToDateTime("2006-01-01");
Console.WriteLine("姓名:{0} 性别:{1} 年龄:{2}",u.Name,u.Sex,u.Age);

}

}
class user
{
private string name;
private string sex;
public string Name
{
set
{
name = value;
}
get
{
return name;
}
}
public string Sex
{
set
{
if (value

你下面的代码弄反了啊,你的代码设置太小或太老的时候进行赋值了:
if (value < Convert.ToDateTime("1900-01-01") || value.Year > DateTime.Now.Year - 3)
birthday = value;
else
Console.WriteLine("you are too yang or too old!!");

应该是:
if (value < Convert.ToDateTime("1900-01-01") || value.Year > DateTime.Now.Year - 3)
Console.WriteLine("you are too yang or too old!!");
else
birthday = value;

呵呵,好像是

就是啊