C# 3.0 C#3.0如何获取只读属性值?

C# 3.0 C#3.0如何获取只读属性值?,c#-3.0,C# 3.0,在C#3.0中,我执行下面的代码来声明DateTime属性和只读的int age属性 public class MyClass{ public DateTime dateOfBirth{ get; set; } public int age { get; } public MyClass(){} public int CalculateAge(){} } 但是,例如,当某人在表单中输入出生日期时,如何获取更新的年龄(只读)?不要存储年龄,而是在执行get o

在C#3.0中,我执行下面的代码来声明DateTime属性和只读的int age属性

public class MyClass{
    public DateTime dateOfBirth{ get; set; }
    public int age { get; }

    public MyClass(){}

    public int CalculateAge(){}
}

但是,例如,当某人在表单中输入出生日期时,如何获取更新的年龄(只读)?

不要存储年龄,而是在执行get of the age属性时计算年龄:

public int Age{get{return 100;}}


但不返回100,而是进行计算。

不存储年龄,而是在执行年龄属性的get时计算年龄:

public int Age{get{return 100;}}


但是,不返回100,而是进行计算。

将用于计算年龄的代码放在age属性中

例如:

public int Age
{
     get
     {
          return (age code here);
     }
}

在age属性中输入用于计算年龄的代码

例如:

public int Age
{
     get
     {
          return (age code here);
     }
}
您需要实现“age”属性,使其在dateOfBirth属性之外工作:

public int age { 
    get {
         return (DateTime.Now - this.dateOfBirth).Days / 365;
    }
}
您需要实现“age”属性,使其在dateOfBirth属性之外工作:

public int age { 
    get {
         return (DateTime.Now - this.dateOfBirth).Days / 365;
    }
}

您不能对此使用自动只读属性。您必须实现该属性。您也可以考虑使用TimeScript而不是int,因为它将更加通用。
public TimeSpan Age 
{ 
    get 
    { 
        return DateTime.Now - this.dateOfBirth; 
    } 
}

您不能对此使用自动只读属性。您必须实现该属性。您也可以考虑使用TimeScript而不是int,因为它将更加通用。
public TimeSpan Age 
{ 
    get 
    { 
        return DateTime.Now - this.dateOfBirth; 
    } 
}

为什么不稍微更改一下您的实现:

public class MyClass
{
    public DateTime DateOfBirth {get; set;}
    public int Age
    {
        get
        {
            return this.CalculateAge();
        }
    }
}

为什么不稍微更改一下您的实现:

public class MyClass
{
    public DateTime DateOfBirth {get; set;}
    public int Age
    {
        get
        {
            return this.CalculateAge();
        }
    }
}

与其让
Age
成为自动属性,不如实现年龄计算器

public class MyClass {
    public DateTime DateOfBirth { get; set; }
    public int Age { 
        get {
            DateTime now = DateTime.Now;
            int age = now.Year - DateOfBirth.Year;
            if(now < DateOfBirth.AddYears(age)) age--;
            return age;
        }
    }
}
公共类MyClass{
公共日期时间出生日期{get;set;}
公共整数{
得到{
DateTime now=DateTime.now;
int age=now.Year-dateof birth.Year;
如果(现在

您可能应该将上述计算重构为一个方法,但上面演示了一个要点。

实现一个年龄计算器,而不是将
Age
作为一个自动属性

public class MyClass {
    public DateTime DateOfBirth { get; set; }
    public int Age { 
        get {
            DateTime now = DateTime.Now;
            int age = now.Year - DateOfBirth.Year;
            if(now < DateOfBirth.AddYears(age)) age--;
            return age;
        }
    }
}
公共类MyClass{
公共日期时间出生日期{get;set;}
公共整数{
得到{
DateTime now=DateTime.now;
int age=now.Year-dateof birth.Year;
如果(现在

您可能应该将上述计算重构为一个方法,但上面演示了一个要点。

正如Svinto所说,计算年龄并在age属性的get方法中返回它。如Svinto所说,计算年龄并在age属性的get方法中返回。如果您想要一个只有您可以分配,但任何人都可以读取的属性,请参见:

public class MyClass
{
    public int Age { get; private set; }
}

然后,您的类可以指定年龄,但其他类只能读取该年龄。

如果您想要一个只有您可以指定但任何人都可以读取的属性:

public class MyClass
{
    public int Age { get; private set; }
}

然后,您的类可以指定年龄,但其他类只能读取年龄。

这不会编译。您不能隐式地将timespan转换为int。@Reed Copsey-dark的代码中没有timespan;它只是在报税单上写着“这里的年龄代码”。用于计算整数年龄值的实际代码放在parantheses中。这不会编译。您不能隐式地将timespan转换为int。@Reed Copsey-dark的代码中没有timespan;它只是在报税单上写着“这里的年龄代码”。用于计算整数年龄值的实际代码在附录中。我甚至不知道可以在没有设置块的情况下声明自动实现的属性(private就可以了),因为您永远无法初始化属性。这是不可能的-编译器会抱怨,正是因为你给出的原因。我甚至不知道可以在没有设置块的情况下声明一个自动实现的属性(private就可以了),因为你永远无法初始化该属性。这是不可能的-编译器会抱怨,正是因为你给出的原因。我试过这么做,但在我的CalculateAge方法中,它说年龄不能分配,因为它是只读的。他说‘不要存储年龄…’我试过这么做,但在我的CalculateAge方法中,它说年龄不能分配,因为它是只读的。他说‘不要存储年龄…’是的-嗯,我只是想展示技术,而不是细节。是的,我只是想展示技术,而不是细节。