CHAPTER 16

image

Properties

Most object-oriented languages support fields and methods. Fields are used to store data, and methods are used to perform operations. This distinction is very useful in organizing classes and making them easy to understand.

Assume that your code needs to fetch the current date and time. It could call a method to perform the operation.

DateTime now = DateTime.GetCurrent();

Since the current time is conceptually a single value, it maps well to a field. It would be much nicer if you could write the following:

DateTime now = DateTime.Current;

But to write that, you would need to have a construct that looks like a field but instead ...

Get A Programmer's Guide to C# 5.0, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.