What is the difference between properties and methods in C??

Methods correspond to what is also called procedures.

Properties look to the caller like ordinary variables that can be read or written.In reality, however, the call executes code sequences that perform additional functions, such as logging or a range check.

An example of an array whose individual entries are accessed through properties:

The calls look quite simple:

Service_ErrorMemory = new ErrorArray("MAS90_SV");nif (Service_ErrorMemory[errpos == Error) return;n-nService_ErrorMemory[errpos = Error;

In contrast, a lot happens when the access is executed:

public byte this[ErrorPos    position-n-get-n-n-n-if        (position == null) return 0;n-n-Pred        pred = new Pred(position);n        ErrorEntry entry = list_of_errors. Find(pred.is_in);        n-n-n-if (entry == null) n -n            return 0; n        -n        -n-else-n-return            entry.wert;n-n-set-n-n-n-if        (position == null) return;-n-pred        pred = new pred(position);        ErrorEntry entry = list_of_errors. Find(pred.is_in);        n-n-if (entry == null)-n---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------                Add(new ErrorEntry(position, value));n n n .n        else        .n (value            == 0)                Remove(entry); . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .    

Unfortunately, something extensive, but I wanted to take an example from practice.

The highlight: The index for access is actually an object that contains several components.2 components in this object actually address a 2-dimensional array that contains mostly zeros. And the data is actually kept in a list. Index positions that do not appear in this list are, by definition, zero. This saves a lot of storage space.

Properties would be expendable as a language, this functionality could also be achieved by means of methods.But you have the conventional spelling as with the use of variables, achieves a more compact, readable representation and can easily turn a variable into a property afterwards if necessary.

Without features, you couldn’t write the following in such a compact way:

SumMeld_ErrorMemory[Line_SumMeld_ErrPos &= unchecked(byte)-0x01);

Properties can also be used to easily implement read-only or write-only accesses.

Leave a Reply