Reputation: 5615
Long story short, if I do this:
string myV = Assembly.GetExecutingAssembly().GetName().Version.ToString();
Will something ever be null? I read the msdn and it doesn't specify the GetName()
and Version
parts.
Upvotes: 8
Views: 221
Reputation: 152624
It's technically possible for that field to be null:
var name = Assembly.GetExecutingAssembly().GetName();
name.Version = null;
Console.WriteLine(name.Version == null); // true
But I can't think of any circumstances in which it would be null. Since it's trivial to check I would just add a null check and throw a custom exception if appropriate if it is null, since diagnosing a NullReferenceException
can be difficult because you don't get any indication as to what is null other than the stack trace.
Upvotes: 5
Reputation: 7017
Version will always be there.
Each assembly has a version number as part of its identity.
https://msdn.microsoft.com/en-us/library/51ket42z(v=vs.110).aspx
By the way, if you are using C#6, in similar cases when not sure about what method returns you should consider using null propogation operator "?.". By doing that you would make sure that it never throws null reference error.
Worst that could happen is that resulting string would be null.
string myV = Assembly.GetExecutingAssembly()?.GetName()?.Version?.ToString();
Upvotes: 4