Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does C# generate GUIDs for Classes? [duplicate]

Apparently C# (or Visual Studio) generates a GUID for each class. I can get the GUID using the following code:

Type myType = typeof(myObject);
Guid myGuid = (Guid)myType.GUID.

My question is when does this GUID change. Is it generated based on a code change or is it based on class creation? I'd appreciate a point towards some relevant documentation.

Thanks

like image 759
Michael Shnitzer Avatar asked Apr 14 '26 08:04

Michael Shnitzer


1 Answers

I don't think there is documentation on how C#/.Net "automatically" generates the GUID for type.

If you need the Guid to stay persistent (or control it in some other way) you should use GuidAttribute instead of letting system to create one for you:

[System.Runtime.InteropServices.GuidAttribute("00000000-0000-0000-feed-000000000000")]
class Test1{};
like image 169
Alexei Levenkov Avatar answered Apr 16 '26 01:04

Alexei Levenkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!