views:

34

answers:

2

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 LINQ query with

... select new Class1{...} 
... select new Class2{...}
A: 

For LINQ to Objects this makes no difference. Both public variables and public properties can be used. When using LINQ to SQL this custom class can be used in projections (selects) without any problem.

Steven
+3  A: 

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.

Andrew Hare
Furthermore, if you wish to eventually add functionalities to the getter setter functions of your fields, you'll need to go along with properties, and in some cases, I don't know as for Linq, this breaks the code and you need to perform refactoring. In such cases, your code might cause unexpected exceptions.
Will Marcouiller