简体   繁体   English

C#Getter / Setter无法正常工作

[英]C# Getter/Setter not working

I am rather the Java-Guy, so handling getters/setters in C# is a little new to me. 我更喜欢Java-Guy,所以用C#处理getter / setter对我来说有点新。 Why is this not working? 为什么这不起作用?

public String lastmodified {
            get { return this.lastmodified; }
            set 
            { 
                long ms = Int64.Parse(value);
                var date = new DateTime(1970, 1, 1).AddSeconds(ms);
                date.ToLocalTime();
                this.lastmodified = date.ToString("HH_mm_yyyy");
            }
        }

value is in this cade a String which goes like 1987123019 该课程中的是类似于1987123019的字符串

Because you are assigning property itself on the last line. 因为您是在最后一行分配属性本身。 You should use either different casing or underscores to distinguish properties or fields. 您应该使用不同的大小写或下划线来区分属性或字段。

I would recommend standard C# naming conventions. 我会推荐标准的C#命名约定。 Also save the data in their native format. 还以原始格式保存数据。 And also what driis said. 还有德里斯所说的话。

private DateTime lastModified;
public String LastModified {
        get { return lastModified.ToString("HH_mm_yyyy"); }
        set 
        { 
            long ms = Int64.Parse(value);
            var date = new DateTime(1970, 1, 1).AddSeconds(ms);
            date = date.ToLocalTime();
            lastModified = date;
        }
    }

This will get you a StackOverflowException, since you are calling the property setter recursively in the last line of set (and get , for that matter). 这将为您提供一个StackOverflowException,因为您是在set的最后一行中递归调用属性setter(对于该问题,它是get )。 You need to assign the value to a field of a class, and read it from that field in the getter. 您需要将值分配给一个类的字段,并从getter中的该字段读取它。 Your current code simply calls into the same property accessor method infinitely, until you run out of stack space. 您当前的代码只是无限地调用相同的属性访问器方法,直到耗尽堆栈空间。

Common C# naming conventions suggests PascalCasing for property and method names, and camelCasing for instance variables, possibly prefixed with an underscore. 常见的C#命名约定建议为属性和方法名使用PascalCasing,为实例变量建议使用camelCasing,并可能带有下划线。

This code should work: 此代码应工作:

private string lastModified; // instance variable
public string LastModified 
{
    get { return this.lastModified; }
    set 
    { 
        long ms = Int64.Parse(value);
        var date = new DateTime(1970, 1, 1).AddSeconds(ms);
        date = date.ToLocalTime();
        this.lastModified = date.ToString("HH_mm_yyyy");
    }
}

Also, "Not working" is kind of broad; 同样,“不工作”的含义很广泛。 but I am going to guess that you will see that the ToLocalTime method does not get applied to the date. 但是我猜想您会看到ToLocalTime方法没有应用于日期。 This is because DateTime in C# is immutable, so it cannot be changed after construction. 这是因为C#中的DateTime是不可变的,因此构造后不能更改。 You will need to assign your date variable to the result of ToLocalTime() : 您将需要将date变量分配给ToLocalTime()的结果:

date = date.ToLocalTime();

For a property procedure, the getter should return the value of the class member variable holding the "wrapped" value. 对于属性过程,getter应该返回持有“包装”值的类成员变量的值。 For a setter, the inbound value should be assigned to that class member variable. 对于设置器,应将入站值分配给该类成员变量。 What you're doing makes the property self-referential. 您正在执行的操作使属性成为自引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM