Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly define class properties?

When defining a new class within a project what is the correct/best practice for doing so? In the past I have created classes such as:

  public class MyClass
  {
      public string FirstName  {get; set;}
      public string LastName  {get; set;}
  }

Normally I’d use a class such as this for the creation of collections within a project.

However as I continue to learn and read more about c# sharp I see examples where classes are defined as:

    class MyClass //not set to public
    {
        private string  _firstName; //first defined as fields
        private string _lastName;

        public string FirstName  // then defined as properties 
        {
            get { return  _firstName; }
            set { _firstName = value; }
        }
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }
    }

Is the first approach incorrect in definition or is this an accepted shorthand version within C#? As a best practice should you always first define the class with private fields and then define them as properties using get / set to a value?

I ask because I am self taught in C# and I am trying to improve and well as better understand the proper approach to development and some samples and tutorials out there simply state approaches without a solid explanation as to why one approach is preferred (or should be done) over the other.

Thanks in advance

like image 610
rlcrews Avatar asked Jul 09 '10 18:07

rlcrews


People also ask

How do you define a class property in Python?

In Python, a property in the class can be defined using the property() function. The property() method in Python provides an interface to instance attributes. It encapsulates instance attributes and provides a property, same as Java and C#.

What are class properties?

The collection of properties assigned to a class defines the class. A class can have multiple properties. For example, objects classified as computers have the following properties: Hardware ID, Manufacturer, Model, and Serial Number.

What does a class define the properties and methods for?

a class describes the contents of the objects that belong to it: it describes an aggregate of data fields (called instance variables), and defines the operations (called methods).

How do you define a class example?

A class can also be called a logical template to create the objects that share common properties and methods. For example, an Employee class may contain all the employee details in the form of variables and methods.


1 Answers

The shorthand syntax (auto implemented properties) in your first example was introduced in C# 3.0, and was not valid before then. The compiler actually converts them to the full form with backing fields.

Before C# 3.0, the only correct way to define properties was with backing fields.

Even with C# 3.0, if you want to have any logic in your properties, you need to convert them to use backing fields.

In short - for dumb properties (those that do nothing), use auto properties. They make your code simpler and easier to read and can be converted.

like image 60
Oded Avatar answered Oct 12 '22 13:10

Oded