Possible Duplicate:
An object reference is required for the non-static field, method, or property
I have a non-static field:
private IDictionary<string, string> _mappings =
new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
that I wnat to use inside of a action like this:
public static string GetMimeType(string extension)
{
//...
return _mappings.TryGetValue(extension, out mime) ?
mime : "application/octet-stream";
}
The compiler is complaining:
An object reference is required for non-static field, method, or
property in the return statement.
How can I reference this field?
You cannot access instance members from static members so you have 2 choices.
statickeyword)statickeyword)The one you choose will depend on whether the field should be shared across all instances or not.