0

I am using the FileHelpers nuget to read the files. It works as excepted but it throws me a warning when I tried to debug in Visual Studio.

How to get rid of warning CS0649: Field 'Orders.Freight' is never assigned to, and will always have its default value null ?

 class Orders : INotifyRead
{
    [FieldFixedLength(10)]
    public string Freight;
    public void BeforeRead(BeforeReadEventArgs e)
    {
        if (e.RecordLine.StartsWith("Machine"))
            // ||
            // e.RecordLine.StartsWith("-"))
            e.SkipThisRecord = true;
    }
    public void AfterRead(AfterReadEventArgs e)
    {
        //  we want to drop all records with no freight
        if (Freight == "_raw")
            e.SkipThisRecord = true;
    }
}
Bobby
  • 21
  • 5
  • Maybe I don't understand but how about just assign a default value to Freight? – blins Sep 16 '16 at 21:14
  • 2
    "How to get rid of warning CS0649..." Very simple google search gives you the exact answer to your question, care to at least search next time? – Camilo Terevinto Sep 17 '16 at 01:19

3 Answers3

4

No, do not explicitly assign a default value to Freight.

The warning is legitimate, because you never really assign a value to the field.

You do not assign a value, because the field gets populated by magic. (Incidentally, that's why I do not like magic; but that's a different story altogether.)

So, the best approach is to acknowledge the fact that the warning is legitimate but accounted for, and to explicitly suppress it.

So, take a look at the documentation of the #pragma warn directive: https://msdn.microsoft.com/en-us/library/441722ys.aspx

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • I will humbly edit or delete my answer if you can help me understand better what you are meaning to convey by the statement "because the field gets populated by magic". I was merely stating the most direct answer in the sense the `... = null` conveys the intent (assuming OP wants `Freight` to be null by default) and thus eliminates the warning rather than just hiding it. – blins Sep 16 '16 at 23:04
  • "magic" <--> "reflection". When you use Reflection to assign a variable (and only use reflection), the compiler cannot detect the variable is assigned, since assignment is only done at runtime. – Thumper Sep 16 '16 at 23:53
  • @blins if there was no magic involved, then setting Freight to null would be the right thing to do in response to the warning. But there is magic involved: the field gets populated by the FileHelpers nuget without being explicitly referenced for writing. Therefore, setting it to null is misleading, as it may lead one to believe that the field does in fact need initialization in code (it doesn't) and that it will in fact have a value of `null` (it won't.) The warning suppression alerts the reader as to what is in fact going on behind the scenes. – Mike Nakis Sep 17 '16 at 00:14
1

For the sake of completeness, I'm just going to combine blins' answer and Mike's answer - nothing original, just trying to help the next person who runs across this page.

Per blins: You may set the value equal to null and the first warning "Field XYZ is assigned to but never used"

public string Freight = null; //or = "", or = default(string) (which is null)

Per Mike, the "magic" he's talking about is Reflection. The variable is assigned to at runtime. This is something the compiler doesn't detect. More on Mike's answer about suppressing the warning found here: Suppressing "is never used" and "is never assigned to" warnings in C#

To suppress warnings for "Field XYZ is never used", you do this:

#pragma warning disable 0169
... field declaration
#pragma warning restore 0169

To suppress warnings for "Field XYZ is never assigned to, and will always have its default value XX", you do this:

#pragma warning disable 0649
... field declaration
#pragma warning restore 0649
Community
  • 1
  • 1
Thumper
  • 525
  • 1
  • 6
  • 21
0

You essentially have two choices and which way to go really depends on the intent (to suggest one or the other is subjective). First, you could eliminate the warning if the design requirement of your Orders type dictates that it should have a null default value.

public string Freight = null;

The above merely clarifies that intent and therefore eliminates the warning.

The alternative is to suppress the warning as the other answers mention. In your case, if the assumption is that the value should have been set via Reflection then this alternative seems reasonable if not preferable in such a case.

blins
  • 2,515
  • 21
  • 32