views:

274

answers:

3

What is the necessity for the GUID attribute? why don't just let the compiler handle this automatically?!

+2  A: 

If the compiler handled this automatically, you'd end up with one of two situations.

  • A new GUID every time you compiled - since GUIDs are supposed to be published, this would fail.

  • Collisions - if the GUID was the same every time, based on (say) a Hash of the name, multiple projects would end up using the same GUID for different purposes.

The existing approach - an explicit GUID gives developers the power to control these as required.

Bevan
So is it a 'Healthy-habit' to always use that attribute manually, or it is only needed in certain circumstances?
Nissim
You will be much better off using it manually every time - otherwise you'll have to recompile all the clients event if only the interface implementation has changed in the COM server.
sharptooth
You only need to add a GUID attribute in limited cases - the primary one being for COM interop, if you're using .NET to write a COM object for use by another system. Depending on what kind of project you have, this might be an everyday event - or an annual one.
Bevan
+1  A: 

Sometimes you want to give certain classes or modules a unique identifier that is constant and hard coded inside your source.

Konstantinos
+1  A: 

You can do it (just omit the attribute) but then the compiler will generate a new GUID on each recompile even if the interface has not changed. That's unfortunate because the users of that interface don't know about the change and will retrieve the interface by it's old GUID and will therefore fail to retrieve it.

sharptooth