Warning. The field is never assigned and will always have a default value of null

I got the following message:

Warning. The field is never assigned and will always have a default value of null.

My code looks (it is simplified, therefore 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 this message is incorrect.

Is this a bug in the C # compiler or am I missing something?

UPDATE Field dbMyProp. This is simplified, but it still raises this warning.

UPDATE2 The following code does not raise this 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 // ...
        };
    }
}
+5
source share
4 answers

Yes, that seems like a compiler flaw. Apparently, he cannot see (does not include) the expression.
The following bbc prints are as expected:

var exp = MyClass.FromMyEntity.Compile();
var mc = exp(new MyEntity { MyProp = "abc"});
Console.WriteLine(mc.MyProp);

, MyClass, .

, , , - () dbMyProp.


, 2 dbMyProp, MyEntity.MyProp .a >

+1

An Expression . ; , . MemberAssignment ( Expression.Bind), .

, . , .

:

static MyClass()
{
    ParameterExpression CS$0$0000;
    FromMyEntity = Expression.Lambda<Func<MyEntity, MyClass>>(
      Expression.MemberInit(
        Expression.New(
          (ConstructorInfo)methodof(MyClass..ctor),
          new Expression[0]
        ),
        new MemberBinding[] {
          Expression.Bind(
            fieldof(MyClass.dbMyProp),
            Expression.Property(
              CS$0$0000 = Expression.Parameter(typeof(MyEntity), "e"),
              (MethodInfo)methodof(MyEntity.get_MyProp)
            )
          )
        }
      ),
      new ParameterExpression[] {
        CS$0$0000
      }
    );
}

, - ; fieldof ( : - string ).

, :

public static readonly Func<MyEntity, MyClass> FromMyEntity = e => new MyClass
{
    dbMyProp = e.MyProp // ...
};

; , " , , ".

+5

.

Pls. .

The following explanations are incorrect.


A warning is displayed because the next undefined value will be used in the next scenerio dbMyProp. 1. Created by MyClass. 2. The instance property is called MyProp. (Which will result in NullReferenceException)

0
source

or am I missing something?

You missed something. Inside the lambda expression, you created a new instance MyClassand assigned its field dbMyProp. But you never assigned an dbMyPropinstance field current inside an instance method.

Check this example:

var instance = new MyClass();
var myProp = instance.MyProp; // BOOM - you never assigned a value to the dbMyProp field
0
source

All Articles