"Using an unassigned variable" - work around?

Now I have known and used this behavior in C # for a long time, and overall I like it. But sometimes the compiler is simply not smart enough.

I have a small piece of code where right now my workaround is not a big problem, but it can be in such cases.

bool gap=false; DateTime start; // = new DateTime(); for (int i = 0; i < totaldays; i++) { if (gap) { if (list[i]) { var whgap = new WorkHistoryGap(); whgap.From = start; //unassigned variable error whgap.To = dtFrom.AddDays(i); return whgap; } } else { gap = true; start = dtFrom.AddDays(i); } } 

The problem I see is what to do if you were to do this with a non-empty structure that did not have a default constructor? Would there be a workaround anyway if start not a simple DateTime object?

+4
source share
5 answers

There is no default constructor in struct . Try:

 struct MyStruct { public MyStruct() { // doesn't work } } 

You may have a static constructor, but you cannot define a default constructor for struct . This is why there is a static Create method for many structures, and why you can say new Point() instead of Point.Empty .

The default constructor of any struct always initializes all its fields with their default values. The static field is empty certian types for convenience. This actually makes a zero difference in performance because they are value types.

+4
source

sometimes the compiler is just not smart enough.

The problem you want to solve by the compiler is equivalent to the stop problem. Since this problem is apparently not solvable by computer programs, we make only a minimal attempt to solve it. We are not doing anything particularly complicated. You just have to live with it.

For more information on why program analysis is equivalent to a stop problem, see my article on whether a method endpoint is available. This is essentially the same problem as determining if a specific variable is defined; the analysis is very similar.

http://blogs.msdn.com/b/ericlippert/archive/2011/02/24/never-say-never-part-two.aspx

What if you needed to do this with a non-null structure that did not have a default constructor?

There is no such animal. All structs, nullable or other, have a default constructor.

Would it be all the same to get around this if the start was not a simple DateTime object?

The expression default(T) gives you the default value for any type T. You can always say

 Foo f = default(Foo); 

and have a legal purpose. If Foo is a value type, it calls the default constructor, which always exists. If it is a reference type, you get null.

+10
source

The compiler does not know that you are guaranteed to set DateTime because of your gap variable.

Just use

 DateTime start = DateTime.Now; 

and get it over with.

Change Better yet, by repeating the code, use

 DateTime start = dtFrom; 
+6
source

It seems to me that your bool gap and DateTime start are the same thing. Try refactoring as follows:

 DateTime? gapStart = null ; for (int i = 0; i < totaldays; i++) { if ( gapStart.HasValue ) { if (list[i]) { var whgap = new WorkHistoryGap(); whgap.From = gapStart.Value ; //unassigned variable error whgap.To = dtFrom.AddDays(i); return whgap; } } else { gapStart = dtFrom.AddDays(i); } } 

[edited to note: please send code samples that will ... oh ... actually compile. It makes it easier.]

[further edited note: you set the gap to true and set the start value for the first time through the loop. Further refactoring on something like this:]

 DateTime gapStart = dtFrom.AddDays( 0 ); for ( int i = 1 ; i < totaldays ; i++ ) { if ( list[i] ) { var whgap = new WorkHistoryGap(); whgap.From = gapStart.Value; //unassigned variable error whgap.To = dtFrom.AddDays( i ); return whgap; } } 
+2
source

Why are you trying to work with language design? Even if the compiler can program your entire cycle in advance, which seems unnecessarily complicated on the part of the compiler, how does he know that exceptions cannot be thrown in parts of your code? You SHOULD assign a start value because you use it later in the code, perhaps before its (if you wish) inevitable assignment.

+1
source

All Articles