Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an assembly have no version?

Tags:

c#

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.

like image 705
Gaspa79 Avatar asked Jun 02 '16 19:06

Gaspa79


People also ask

What is default version of assembly?

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.

What is assembly file version?

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.

What is the difference between assembly version and file version?

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.

What is the order of an assembly version?

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.


2 Answers

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.

like image 112
D Stanley Avatar answered Sep 20 '22 07:09

D Stanley


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();
like image 38
Kaspars Ozols Avatar answered Sep 23 '22 07:09

Kaspars Ozols