"Use of unassigned local variable" in a Generic Method
Use of Unassigned local variable 'model'. Is what error message I'm getting. Its right where I say if (model == null). I'm not sure why its giving me开发者_StackOverflow社区 a compile time error there.. 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;
}
As the error states, 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 it an initial valued first:
T model = default(T);
Note that if T
is a struct type, model == null
can never be true.
You should 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();
}
}
The compiler doesn't 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 just move the new statement to before your deserialization block. That way, you'll either have a new object or the deserialized object just as you have now.
When working with local variables, they must always be assigned a value before being accessed.
The reason for this is because usually, when the developer omits initialization, he relies on the runtime to assign it its default value, but in the case he forgot, this can cause some unnecessary bugs.
In the case of working with generics, and not specifying whether you're expecting a ReferenceType
or a ValueType
, you cannot simply initialize it by assigning null
.
In this case you need to use the default
keyword.
This will initialize the variable to null
for reference types, or assign 0
for numeric value types. For structs, it will initialize each member to its default.
In the example you posted, the comparison with null
lets me assume this method might only be meant to be used with ReferenceTypes, if that is the case, it is best to also add the class
constraint.
Because model
is a local variable, the compiler is giving you that error because it is only assigned in the if statement. If the if statement condition is not true, model
will not be assigned a value. Try giving it a default value of null
or add an else statement and assigning model
inside it.
T model = null;
Local variables are not automatically initialized, but instance variables are.
public class MyClass<T>
{
private T instanceVariable; // automatically initialized
public void MyMethod()
{
T localVariable; // not automatically initialized
}
}
精彩评论