0

i'm learning C# and i can't understand what is the difference:

a) public int value { get; set; }

b) public int value

i think it have more sense

public int value { get {return "data:"+somevalue;}}

  • See [See here](https://stackoverflow.com/questions/5096926/what-is-the-get-set-syntax-in-c). That can help you. Because it's a same question. – Md. Tazimul Islam Raj Sep 25 '21 at 06:13
  • Your "I think it have more sense" code actually makes no sense: you said the property is `int` and then you return a string - it's not possible – Caius Jard Sep 25 '21 at 06:23
  • Public members of a class should use PascalCase, not camelCase. Typically we do not make fields public; if you want to expose data publicly, use properties but also bear in mind that properties should not return arrays. – Caius Jard Sep 25 '21 at 06:24

1 Answers1

0

One is property and another is a field. Equivalent of property in Java is get, set method. So, in nutshell, "public int value" create a field/attribute in your class. Whereas "public int value { get; set; }" will create a private field/attribute and two public methods to get(read) and set(write) that field/attribute in class. So, it gives safer way to access your field/attribute.

divyang4481
  • 1,584
  • 16
  • 32