DEV Community

Cover image for C# Basics - Classes & Properties
Grant Riordan
Grant Riordan

Posted on • Edited on

C# Basics - Classes & Properties

Now we've establised what a variable, lets look at one of the key aspects of .Net, and OOP (Object Orientated Programming)... the Class

What is a Class?

In C# and .NET, almost everything revolves around classes and objects.

A class defines the structure: it can contain fields, properties, and methods.

An object is an instance of a class, giving you access to its members.

Creating a Class

You define a class with the class keyword:

public class User
{
    // class body
}
Enter fullscreen mode Exit fullscreen mode

Typical Order of Items

Typical order of class members in C# are:

  • Fields (private variables)

  • Constructors

  • Properties

  • Methods

You can put the constructor before or after properties, fields, or methods, and the code will compile the same.

That said, there are some common style conventions developers follow for readability.

Fields vs. Properties

Both fields and properties are core building blocks of a class, but they serve different purposes:

Field: a variable declared directly in a class.

Property: provides controlled access to a field (read, write, or compute).

Example:

public class User
{
    // private fields
    private string _firstName;
    private string _lastName;

    public User(string firstName, string lastName)
    {
        _firstName = firstName;
        _lastName = lastName;
    }

    // public read-only properties
    public string FirstName => _firstName;
    public string LastName => _lastName;

    // computed property
    public string FullName => $"{FirstName} {LastName}";
}

Enter fullscreen mode Exit fullscreen mode

Here:

_firstName and _lastName can only be set inside the constructor, (we'll get to to them next).

FirstName and LastName are read-only properties, exposing the values safely from the private fields.

FullName is a "computed" property, built from the others.

Automatic Properties

Automatic properties let you declare a property without writing a backing field or extra logic:

public class User
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string PostalCode { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

They behave just like normal properties:

var user = new User();
user.PostalCode = "PO77 T1K";
Enter fullscreen mode Exit fullscreen mode

Behind the scenes, the compiler creates the hidden fields for you. This is the most common way you’ll see properties used in C#.

When to Use Fields vs. Properties

Fields

Store raw data.

Should almost always be private.

Useful for constants or values only used inside the class.

Properties

Control access to fields.

Can be read-only (get only), writeable (set), or computed.

Provide a safe and flexible way to expose data.

Access Modifiers: Public vs. Private

private → only accessible inside the class.

public → accessible from other classes.

Constructors

A constructor defines how a new object of a class is created and initialized:

public class User
{
    public string Name { get; set; }
    public int Age { get; }
    public DateTime DOB { get; }

    public User(DateTime dob)
    {
        DOB = dob;
        Age = (DateTime.Now - DOB).Days / 365;
    }
}

// usage
var user = new User(new DateTime(1965, 2, 25));
user.Name = "Gary Phillips";

Console.WriteLine(user.Age); // computed in constructor

Enter fullscreen mode Exit fullscreen mode

Here:

DOB is set in the constructor.

Age is calculated once at object creation.

Computed Properties vs. Constructor Values

If you want a live value (e.g., age that updates automatically), use a read-only property instead:

public int Age => (DateTime.Now - DOB).Days / 365;

Enter fullscreen mode Exit fullscreen mode

This way, every time you access Age, it’s recalculated.

👉 Constructors are great for setting defaults or guaranteed values at creation.

👉 Computed properties are better when you need values that always stay up to date.

Top comments (0)