简体   繁体   中英

In Visual Studio 2015 readonly automatic properties can be set and it builds! Is this a bug?

This code

public class Entity
{
    public string First { get; }
    public string Last { get; }
    public Entity()
    {
        this.First = "First name";
        this.Last = "Last name";
    }
}

Will compile in VS2015 yet VS2013 gives errors "Property or indexer 'ScottRickman.Entity.First' cannot be assigned to -- it is read only"

Is this a bug in VS2015?

This is a new feature of C# 6.0 (see section "Getter-only auto-properties"). By default VS2015 uses C# 6.0 compiler, while VS2013 uses C# 5.0 compiler, hence the compile time error occurs only in VS2013.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM