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.
Assembly version number 1254.0 indicates 1 as the major version, 5 as the minor version, 1254 as the build number, and 0 as the revision number.
It's the version number given to file as in file system. It's displayed by Windows Explorer, and never used by . NET framework or runtime for referencing.
AssemblyVersion: Specifies the version of the assembly being attributed. AssemblyFileVersion: Instructs a compiler to use a specific version number for the Win32 file version resource.
Version information for an assembly consists of the following four values : a major and minor version number, and two further optional build and revision numbers. This should only change when there is a small changes to existing features.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With