I have the following code that does not work because "a" is the entered value. But I thought that this would not work even without accessories, but it happened:
class Program { a _a //with accessors it WONT compile { get; set; } static void Main(string[] args) { Program p = new Program(); p._a.X = 5; //when both accessors are deleted, compiler does not //complain about _a.X not being as variable } } struct a { public int X; }
It does not work as "a" - it is a structure. But when I remove the accessors from the _a instance, it works. I do not understand why. Thanks
The main feature of value types is that they are copied, not passed by reference.
When you have a value type and an accessor, you have a value type returned by the method that calls the copy (the following two examples are the same):
ValueType Property { get { return x; } } // Will make a copy of x ValueType Method() { return x; } // Will make a copy of x
, x. , , , .
{get; } accessor, , :
int field;
ValueType field;
, , , .
.
:
a _a;
, .
: , p._a, . , "" "" _a. , getter.
_a
# , , , p._a.X = 5; int xx = p._a.X; xx 5. . p_.a : -)
p._a.X = 5; int xx = p._a.X;
xx
,
_a - ;
a _a { get; set; }
_a .
a _a { }
p._a.X = 5; , p._a a. . , .
p._a.X = 5;
p._a
a
Source: https://habr.com/ru/post/1740120/More articles:How to use a third-party control in view mode? - design-patternsparent asset - androidMatplotlib: building discrete values - pythonPHP FTP Download Thousands of Files - arraysПростая форма проверки. Объектно-ориентированный - javascriptGetting started with C ++ (paradigm shift with Python) - c ++erroneous visual C float / double conversion? - cHow to implement a UITableView with multiple columns and side scrolling - iphoneExcel VSTO → Скрыть/показать ленту на основе другой кнопки ленты - c#Autofill - data-structuresAll Articles