"Using an unassigned local variable" in a generic method

Using the unassigned local variable 'model'. This is the error message I get. Its right where I say if (model == null). I don't know why this gives me a compile-time error. Someone please help.

public static T TryGet<T>(string fileName) where T : new() { T model; using (var storageFile = IsolatedStorageFile.GetUserStoreForApplication()) { using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Open, storageFile)) { if (stream.Length > 0) { var serializer = new DataContractSerializer(typeof(T)); model = (T)serializer.ReadObject(stream); } } } if (model == null) { model = new T(); } return model; } 
+7
source share
4 answers

As the error indicates, you cannot use a local variable until the compiler can prove that it has been assigned a value.

In your case, if your if condition is false, the model variable is never assigned.

You can solve the problem by assigning an initial assessment first:

 T model = default(T); 

Note that if T is a structure type, model == null never be true.

You must change your code to

 using (var storageFile = IsolatedStorageFile.GetUserStoreForApplication()) using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Open, storageFile)) { if (stream.Length > 0) { var serializer = new DataContractSerializer(typeof(T)); return (T)serializer.ReadObject(stream); } else { return new T(); } } 
+15
source

The compiler does not know what to assign until you tell it.

Instead of T model; use T model = default(T);

For more information: http://msdn.microsoft.com/en-us/library/xwth0h0d(v=vs.80).aspx

edit: another option is to simply move the new statement to the deserialization block. Thus, you will have either a new object or a deserialized object, as now.

+3
source

When working with local variables, a value must always be assigned before accessing them.

The reason for this is that usually, when a developer omits initialization, he relies on the runtime to give it a default value, but if he forgot, this can cause some unnecessary errors.

In the case of working with generics and not specifying whether you expect a ReferenceType or ValueType , you cannot just initialize it by assigning null . In this case, you need to use the default keyword .

This initializes the null variable for reference types or assigns 0 for numeric value types. For structs, it initializes each member by default.

In the above example, comparison with null allows me to assume that this method can only be used for ReferenceTypes, if so, it is better to add a class constraint.

+3
source

Since model is a local variable, the compiler gives you this error, because it is assigned only in the if statement. If the condition of the if statement is incorrect, model will not be assigned a value. Try to give it a default value of null or add an else statement and assign it a model .

 T model = null; 

Local variables are not initialized automatically, but instance variables.

 public class MyClass<T> { private T instanceVariable; // automatically initialized public void MyMethod() { T localVariable; // not automatically initialized } } 
+1
source

All Articles