Difference of field and property

  • Thread starter Thread starter robert-y
  • Start date Start date
R

robert-y

Guest
Hi teams,

I am new to programming and am learning reflection now.

Usually, we will write a class like this:

class MyClass
{
private int id;
private string name;
public int ID
{
get { return this.id; }
set { this.id = value; }
}
public string Name
{
get { return this.name; }
set { this.name = value; }
}
}

Then ,when i try to get field, it goes like this:

Int32 id
System.String name





But if i only write property,

class Class2
{
public int ID { get; set; }
public string Name { get; set; }
}

I can still get two fields, although looks strange:

Int32 <ID>k__BackingField
System.String <Name>k__BackingField

So, my question is:

Since I write fields or not, there will be fields generated, so why should I write the fields myself? What is the significance of the field?

Thanks in advance.

Robert

Continue reading...
 
Back
Top