开发者

when creating custom class to be used by LINQ, does it matter if it pure variable or should it always be properties?

开发者 https://www.devze.com 2022-12-22 14:29 出处:网络
what would be the difference between this class Class1 { public string prop1 { get; set; } public string prop2 { get; set; }

what would be the difference between this

class Class1
{
    public string prop1 { get; set; }
    public string prop2 { get; set; }
    public string prop3 { get; set; }
    public string prop4 { get; set; }
}

and this

class Class2
{
    public string var1;
    public string var2;
    public string var3;
    public string var4;
}

when executing a开发者_StackOverflow社区 LINQ query with

... select new Class1{...} 
... select new Class2{...}


This doesn't matter as far as LINQ is concerned but if you are concerned about proper object oriented design, you should use properties. If you don't add proper encapsulation around the state of your type (i.e. the fields) you are creating potential problems for yourself in the future as you will be unable to verify or control the state of your type since the consumers of this type will have free reign to change things without using proper channels (i.e. a property or method).

All of that being said, however, LINQ queries will work just fine either way.

0

精彩评论

暂无评论...
验证码 换一张
取 消