I have got following message:
Warning: Field is never assigned to, and will always have its default value null.
My code looks like (it is simplified, so useless):
public class MyEntity
{
// ...
public string MyProp { get; set; }
}
public class MyClass
{
string dbMyProp;
public string MyProp { get { return dbMyProp.Replace("a", "b"); } }
public static readonly Expression<Func<MyEntity, MyClass>> FromMyEntity = e => new MyClass
{
dbMyProp = e.MyProp // ...
};
}
I think that the message is not true.
Is it a bug in C# compiler or I have missed something?
UPDATE The field is dbMyProp. It is simplified but it still produces this warning.
UPDATE2 The following code does not produce such warning:
public class MyClass2
{
string dbMyProp;
public string MyProp { get { return dbMyProp.Replace("a", "b"); } }
public static MyClass2 FromMyEntity(MyEntity e)
{
return new MyClass2
{
dbMyProp = e.MyProp // ...
};
}
}
Yes, it seems a shortcoming of the compiler. It apparently cannot see through (does not include) the Expression.
The following prints “bbc” as expected:
I tried a few tricks like a private constructor on MyClass but the warning remains.
It seems the compiler applies a more conservative algorithm, only checking if any (normal) member assigns to
dbMyProp.You are confusing things by having 2 different elements with the name
dbMyPropbutMyEntity.MyPropis never written to here.