---
title: "Set Default Value to Property in C#"  
description: "You can set default values to properties in several ways, depending on the context and the type of the property"  
author: "Ravi Vishwakarma"  
published: 2024-06-10  
updated: 2025-04-22  
canonical: https://www.mindstick.com/blog/304334/set-default-value-to-property-in-c-sharp  
category: "c#"  
tags: ["c#", ".net", ".net core", ".net core 6"]  
reading_time: 3 minutes  

---

# Set Default Value to Property in C#

In C#, you can set **[default values](https://www.mindstick.com/blog/10881/default-values)** to effects in several ways, trusting the context and the type of the [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp).

Here are a few common [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best) to set default values to [properties](https://www.mindstick.com/articles/23331/nootropics-7-different-types-and-their-unique-properties) in a class:

#### 1. Default Values in Field Initializers

You can initialize the property directly where it's declared.

```cs
public class Person
{
   public string Name { get; set; } = "Unknown";
   public int Age { get; set; } = 0;
   public bool IsStudent { get; set; } = false;
}
```

#### 2. Default Values in Constructors

You can set default values in the constructor of the class.

```cs
public class Person
{
   public string Name { get; set; }
   public int Age { get; set; }
   public bool IsStudent { get; set; }
   // Constructor to set default values
   public Person()
   {
       Name = "Unknown";
       Age = 0;
       IsStudent = false;
   }
}
```

#### 3. Default Values with Auto-Properties (C# 6 and later)

[Starting](https://www.mindstick.com/blog/12024/things-you-need-to-know-when-starting-your-own-business) from C# 6, you can use auto-property initializers to set default values.

```cs
public class Person
{
   public string Name { get; set; } = "Unknown";
   public int Age { get; set; } = 0;
   public bool IsStudent { get; set; } = false;
}
```

#### 4. Default Values with Backing Fields

You can also use backing fields to set default values.

```cs
public class Person
{
   private string _name = "Unknown";
   private int _age = 0;
   private bool _isStudent = false;
   public string Name
   {
       get { return _name; }
       set { _name = value; }
   }
   public int Age
   {
       get { return _age; }
       set { _age = value; }
   }
   public bool IsStudent
   {
       get { return _isStudent; }
       set { _isStudent = value; }
   }
}
```

#### Example Usage

Here’s how you can use the Person class with default values:

```cs
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main()
        {
            // Using the default constructor
            Person person1 = new Person();
            Console.WriteLine($"Name: {person1.Name}, Age: {person1.Age}, IsStudent: {person1.IsStudent}");

            // Setting properties explicitly
            Person person2 = new()
            {
                Name = "John",
                Age = 25,
                IsStudent = true
            };
            Console.WriteLine($"Name: {person2.Name}, Age: {person2.Age}, IsStudent: {person2.IsStudent}");
            Console.ReadLine();
        }
    }
}
```

#### Explanation

1. **Default Values in Field Initializers:** Properties are initialized directly where they are declared using the = operator.
2. **Default Values in Constructors:** Default values are set inside the constructor of the class. This ensures that all [instances](https://answers.mindstick.com/qa/92508/how-can-you-hide-the-sql-server-instances) of the class will have the specified default values unless they are explicitly overridden.
3. **Default Values with Auto-Properties:** This [approach](https://www.mindstick.com/interview/985/what-are-the-approaches-that-you-will-follow-for-making-a-program-very-efficient) simplifies the syntax for setting default values for properties.
4. **Default Values with Backing Fields:** This approach provides more control over the properties and allows for additional logic when getting or setting the property values.

These methods allow you to [ensure that your](https://www.mindstick.com/articles/23198/3-steps-to-ensure-that-your-client-portal-is-impeccable) properties have [meaningful](https://yourviews.mindstick.com/story/1909/6-meaningful-ways-to-celebrate-christmas-this-year) default values, reducing the chances of null or uninitialized [values in your](https://yourviews.mindstick.com/view/85279/importance-of-moral-values-in-your-day-to-day-life) class instances.

---

Original Source: https://www.mindstick.com/blog/304334/set-default-value-to-property-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
