D
D
Dmitry2015-01-03 21:52:22
C++ / C#
Dmitry, 2015-01-03 21:52:22

When to use fields and when to use properties in C#?

Guys, tell me! When to use fields and when to use properties?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Peter, 2015-01-03
@petermzg

1. If you need to restrict access to data for writing or reading, then properties
2. If you need easier access through reflection, then properties
3. If you implement an interface, then properties
In other cases, it doesn’t matter what you prefer.

M
MrDywar Pichugin, 2015-01-03
@Dywar

.NET Framework General Reference -> Class Library Design Guide -> Member
Development Rules Field
Development Property Development

V
Vitaly Vitrenko, 2015-01-03
@Vestail

In fact, you will always use margins!
Properties are simply field accessors that look like fields.
If the field is not readonly or const, then in order to comply with encapsulation, they must be made private, and getters and setters should be used to access them. Properties simply provide syntactic sugar for easy implementation of the latter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question