Should all methods that have a return value turn into a property?

I was just thinking about it, and since .NET introduced the properties, there is always a situation where you want to leave your code as a method that returns a value, not a readonly property.

+3
source share
4 answers

No. I would recommend taking a look at the Microsoft Property Usage Guides :

Class library designers often need to decide between implementing a class member as a property or method. In general, methods represent actions and properties represent data. Use the following guidelines to help you choose between these options.

  • , . , .
  • , :
    • - , Object.ToString.
    • , .
    • get accessor .
    • .
    • . , .
    • , , .
    • . , , . , . , , , , . , . 2n + 1 .
+10

, , . , .

, , , . , . this, "" API. .

+2

, .

0

. , - , , .

, , .

, , , . , , - . , , - :

foreach( var x in myClass.PropertyThatReturnsArray )
{
}

. , , , , :

var foo = myClass.GetTheArray();

foreach( var x in foo )
{
}

( , fxcop: http://msdn.microsoft.com/en-us/library/0fss9skc(VS.80).aspx).

.:)

0

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


All Articles