# New Rule: Use auto properties

**URL:** <https://community.sonarsource.com/t/new-rule-use-auto-properties/138952>\
**Category:** New rules / language support\
**Tags:** csharp\
**Created:** [April 18, 2025, 6:18am UTC](https://community.sonarsource.com/t/new-rule-use-auto-properties/138952 "2025-04-18T06:18:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Corniel](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/corniel/32/10101_2.png) [@Corniel](https://community.sonarsource.com/u/Corniel)\
**Post date:** [April 18, 2025, 6:18am UTC](https://community.sonarsource.com/t/new-rule-use-auto-properties/138952/1 "2025-04-18T06:18:24Z")

</div>

The use of properties in .NET relies on the use of backing fields. To reduce the mental load, C# allows use of auto properties and the [`field`](https://learn.microsoft.com/dotnet/csharp/language-reference/proposals/field-keyword) keyword (since C# 14/13-preview). When possible, those auto properties should be applied.

Obviously, this rule should benefit from an additional code-fix.

```auto
class NonCompliant
{

    public int Prop // Noncompliant {{Use auto property}}
    {
        get { return m_Prop; }
        set { m_Prop = value; }
    }
    private int m_Prop;

    public string WithLogic // Noncompliant {{Use auto field instead of m_WithLogic}}
    {
         get => m_WithLogic;
         set => m_WithLogic = value?.Trim();
    }
    private string m_WithLogic;
}

class Fixed
{

    public int Prop { get; set; }

    public string WithLogic
    {
         get => field;
         set => field = value?.Trim();
    }
}

class Compliant
{
    public string UsingField
    {
         get => field;
         set => field = value?.Trim();
    }

    public int Prop // Compliant, the backing field has a different type
    {
        get { return m_Prop ??= Init(); }
    }
    private int? m_Prop;

    private static int Init() => 42;
}

```

---

<div class="post-metadata">

**Author:** ![Mary](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/mary/32/59106_2.png) [@Mary](https://community.sonarsource.com/u/Mary)\
**Post date:** [April 25, 2025, 2:00pm UTC](https://community.sonarsource.com/t/new-rule-use-auto-properties/138952/4 "2025-04-25T14:00:31Z")

</div>

Thanks @Corniel,  
I added a ticket (internal access only) to our backlog with a rule proposal!
