I have a static C# class that exposes some pre known Guids via properties
I used the old fashioned property syntax e.g a private declaration that declares the guid and a public property that exposes it. e.g. something like
private static Guid aGuid = new Guid("l1....");
public static Guid AGuid { get { return aGuidl }}
But would be less code just to have the automatic property e.g.something like
public static Guid AGuid { get{ return new Guid("11...") }}
The question is, is the former more verbose method more efficient or is the C# compiler clever enough not to create a new Guid every time if I use the later approach.
Caching (your 1st sample) would be a little more efficient here.
The compiler won’t do this as an optimization.
I just did a quick check with Ildasm.
It’s not an unreasonable assumption, we all know the Guid constructor should yield the same value every time. But the compiler cannot assume this, there would have to be a list of special types, probably not worth the trouble.