Fields and Properties Best Practice in C #

Dear everyone, which one is best practice using C # and why?
1.

private string name; public string Name { get { return name; } set { name = value; } } 

2.

 public string Name { get; set; } 

3.

 protected string name; public string Name { get { return name; } set { name = value; } } 

4. Please add ...

+4
source share
9 answers

Fragments 1 and 2 are both good. The second is just a more convenient way to record the first when you don't need access to the base field.

Snippet 3 , however, should generally be avoided (unless you have a specific reason to use it), since the fields should almost always be closed. If you need to provide another way to set the field for descendant classes (which is unlikely), you can use methods or another property.

Remember that a protected member is essentially a slightly more limited public member, as client code can access it if it is in a descendant class. This means that client code can be bound directly to the implementation of the class, and not to its interface, which is bad!

+13
source

Start with the second snippet, i.e.

 public string Name { get; set; } 

Change it to the shape of the first snippet when you need to add validation, or do some logic when the value is set.

I would avoid the last option, as this would allow the first-class class to directly access the backup field, which links you to a specific implementation (this also means that your fine check can be circumvented)

+11
source

Usually the standard format is:

 private string name; public string Name { get { return name;} set { name= value; } } 

Fields will be closed as standard practice. Your properties will have a public security public .

0
source

The provision of public property is to avoid providing public class fields to consumers. And properties allow you to control how consumers use it (by getting and installing).

The 1st option is fine, in the second example (using open and protected) you just break it ...

0
source
 public string Name {get;set} 

The second example is an automatic property and is usually used when you do not have logic in getters and seters. This is also a short form for writing the first example.

In the third example, there is a protected support field, so the subprogram field can be accessed directly by a subclass.

Choose a format depending on your purpose. Also, consider asking other team members if a consistent format already exists.

0
source

For the most part, in the applications that I wrote, if the getter and setter are no more complicated than your examples, you probably want to minimize the code as much as possible for readability, so a shortened approach is very useful. On the other hand, if you are performing more complex logic in getters and setters (for example, checking or parsing of some kind), you need to make sure that no custom class is bypassing your logic.

Also, as Binary Worner notes in a comment, it depends on what you need and what you want. You know the difference between private , protected and public , and thus you also know what you can and cannot do with your class, depending on which one you choose. Which one is "best" depends entirely on what behavior you want (and don't want) to allow other classes to use this one.

0
source

Write automatic properties (NΒΊ 2) by default, if your getter / setter does not have any logic, otherwise the property (or field) should not have anything - you must agree that the public property that directs access to its private field - This is the same as an open field (or auto-propensity).

0
source
  #region LinkURL private string _LinkURL = String.Empty; public string LinkURL { get { return _LinkURL; } set { _LinkURL = value; } } #endregion LinkURL 

This method is easy to generate code. For example, if you have Excel with properties or a db table that you will need to implement in the class, you can check this regular expression tip:

0
source

What is the best way to get a read from two properties by field or by get operation? my own conclusion is to get from the field, since this is the information you want to process, you just need to ask if I'm wrong.

  private string _key; private string _subKey; private string _fullKey; public string SubKey { get { return _key; } set { _key = value; } } public string SubKey { get { return _subKey; } set { _subKey = value; } } 

1.

 public string FullKey { get { return _subKey + _key; } } 

2.

 public string FullKey{ get { return SubKey + Key } } 
0
source

Source: https://habr.com/ru/post/1301330/


All Articles