Hello. Torture in uni C # ...
There was a laboratory work; several classes describing geometric figures implement an interface with the Square () method, which returns the area of ​​the figure. But the task from the following laboratory:

To set the previous laboratory work in all classes of specific figures, the method of calculating their area should be replaced by the polymorphic property calculating the area.

I correctly understand that the task formulated is not entirely correct, and it would be more correct to say

... property that contains the value of the area.

Well, by the type Length array. And to calculate this same area can be, for example, in the designer.

Or in Sharp there are some special properties that can count?

  • Square is a square in the sense of "Lenin Square", and not a "figure square." Area in the sense in which you need - Area. - VladD
  • Just got here. Well, they closed the question in the meta - and to hell with it. I will no longer offer a normal forum. - user6550 10:49 pm
  • @klopp: The normal forum is a bit oftopic (HC itself is also like a forum), I thought more about the chat. Chat offers were also there, but did not take off. - VladD

2 answers 2

If you are familiar with OOP and its standard terminology, then you should probably be aware that the field and the property are different concepts that look alike. In your question, you obviously call the property that in OOP is called a field. However, the property is something else. A property is essentially syntactic sugar over a method (or a pair of methods) for returning a certain value. More specifically:

class Person { // это поле. Не свойство private int _someField; // а вот это уже свойство public int Age {get; set; } } 

So far both of them look similar to each other (equal to the fact that we spent a little more than characters on property declaration). However, the difference, and quite significant, is still there. As already mentioned, the property is syntactic sugar, and the above example for the compiler will be something as follows:

 class Person { // то же самое поле, ничего нового private int _someField; // весь код, описанный ниже // представляет собой то, // во что раскрывается свойство private int age; public int get_age() { return age; } public int set_age() { return age; } } 

What is it for? You can do the usual field and not fool yourself a head. It turns out you need. A field is simply a "piece" of data, or even a "guts" of a class, and the end user can do anything with these "guts" just give him a free rein. For example, if this field is a person's age, then the end user can easily make this age negative, or very large, which can break the internal logic of the program. Therefore, it would be more convenient to wrap the field in some kind of logic that would limit the user's riot (by the user here is meant the programmer who will work with this class). For example:

 class Person { private int _age; public int Age { get { return _age; } set { if(value <= 0 || value > 120) throw new SomeException("A terrible exception was thrown!"); } } } 

Already more reliably - the user can not break the logic associated with age. The same could be done with a private _age field and a couple of public methods GetAge and SetAge, but it looks more elegant (an impression is created that Age is still a field) and less code is spent. Plus, you can always make public only a getter or only a setter, which is not the case with a field.

Now regarding your question - the property that returns the area of ​​the figure should just be something like

 // вариант для прямоугольника public override double Square { get { return _width * _height; } } 

where _width and _height are the fields in which the length and width are stored respectively. It is not a good idea to calculate the area in the constructor, and without this there is something to do, especially since if the size of your figure changes during the life of the object, then it is completely useless.

ZY By the way, a little funny either a bug or a feature of the language: The following, seemingly quite correct code will not be compiled:

 public class Foo { public int Prop { get; set; } // ошибка public int get_Prop() { return 10; } } 

In the line marked with a comment, the compiler will tell you that the get_Prop method has already been declared. This is obviously due to the fact that the Prop compiler decomposes the property into a couple of get_Prop and set_Prop methods (plus the corresponding field).

  • Cool, thanks. @DreamChild, do you understand all languages? You tell me about both php and java and sharps are answered ... - sinedsem
  • @Dazar, about sharpe - yes, this is my main language. As for php or java, there I can say something only on more or less general issues. However, if we talk specifically about this issue, then most of the above on the topic will be valid for many other languages, except with a slightly different syntax - this is more about the PLO, rather than specifically C #. - DreamChild

@Dazar , in the language level, accessors - setters and getters are implemented at the language level. You can declare a property that, when accessing it, will itself calculate the value:

 public int width; public int height; public int space { get { return width * height; } } 

http://msdn.microsoft.com/ru-ru/library/aa287786(v=vs.71).aspx

(specifically in this example, of course, there are no overflow guarantees or that the width and height will be set at the moment of receiving the space)