O'Reilly logo

A Programmer's Guide to C# 5.0, 4th Edition by Nick Wienholt, Eric Gunnerson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required