C# const, readonly

Featured image

Git Source


const, readonly 🌈

const :

readonly :

private readonly Person person;// = new Person("홍길동", 24);
private static readonly Person person2 = new Person("심봉사", 24);
private const float Pi = 3.14f;

// constructor
public Program()
{
    person = new Person("홍길동", 24);
    // Pi = 3.1415f; (X)
    // person2 = null; ( X )
}

static Program()
{
    person2 = null;
}

class Person
{
    public string Name { get; set; }
    public int Age { get; set; } 

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}