-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Closed
Labels
Description
Input code
class StructInitPropertiesTest
{
class TypeA
{
public int A { get; set; }
public int B { get; set; }
}
struct TypeB
{
public int A { get; set; }
public int B { get; set; }
}
struct TypeC
{
public int A { get; init; }
public int B { get; init; }
}
static TypeA TestA()
{
return new TypeA {
A = 1,
B = 2
};
}
static TypeB TestB()
{
return new TypeB {
A = 1,
B = 2
};
}
static TypeC TestC()
{
return new TypeC {
A = 1,
B = 2
};
}
}Erroneous output
internal class StructInitPropertiesTest
{
private class TypeA
{
public int A { get; set; }
public int B { get; set; }
}
private struct TypeB
{
public int A { get; set; }
public int B { get; set; }
}
private struct TypeC
{
public int A { get; init; }
public int B { get; init; }
}
private static TypeA TestA()
{
return new TypeA {
A = 1,
B = 2
};
}
private static TypeB TestB()
{
// No error but could still be improved
TypeB result = default(TypeB);
result.A = 1;
result.B = 2;
return result;
}
private static TypeC TestC()
{
TypeC result = default(TypeC);
result.A = 1; // Error
result.B = 2; // Error
return result;
}
}Error message: Init-only property or indexer 'StructInitPropertiesTest.TypeC.A' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor.
Details
- Product in use: e.g. ILSpy 9.0 VS extension