C# Auto Properties and Mixed Access Modifiers

In this article, we will discuss two features of the .Net framework that help us more quickly write cleaner code.

First, The Old Method

Before auto properties, if I wanted to create a new property in a class with get and set methods, I could write the lengthy:

void getMyInt(){
    return myInt;
void setMyInt(newInt){
    myInt = newInt;

This is a lot to write out for each variable in our code. A simpler method is the auto property method.

C# .Net Auto Property Feature

Utilize the auto property feature by typing [prop] then [tab][tab]. Which will create a line such as:

public int myProperty {get;set;}

Neither of these code snippets will allow us to initialize values for our int in their current form. In order to set a value we can rewrite our property declaration as so:

public int myProperty {get;set;} = 100;

Except now we have introduced another issue potentially. What if in our application we only want the get method to be publicly accessible and the set method to be private? To over come this we can actually explicitly declare mixed access within this short property declaration.

C# .Net Mixed Access Modifiers

In order to declare mixed access modifiers within our property we can modify the line of code to:

public int myProperty { get; private set;} = 100;

Easy right? Now we have a public get method for myProperty, the value can only be set by the class and we have initialized the integer with a value needed for the application.

I hope this was helpful, happy coding.