Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create or generate an invalid System.Guid in .Net?

Tags:

c#

.net

guid

vb.net

Can somehow an invalid System.Guid object be created in .Net (Visual Basic or C#). I know about Guid.Empty method but it is not what I am searching for. For example with invalid symbols or spaces or being too short or longer than an valid one etc. Thanks!

like image 664
Kalin Krastev Avatar asked Jan 31 '14 14:01

Kalin Krastev


2 Answers

Can a Guid be created with invalid symbols or spaces or being too short or longer than an valid one etc?

No - a Guid is just a 128-bit number that can be represented in 5 groups of hexadecimal numbers. There's no way for the string representation of a Guid to contain anything other than 32 hex characters (and possibly 4 hyphens or a few other formatting characters).

You can create a string that does not represent a valid Guid, but there's no way to put that into a Guid object.

like image 199
D Stanley Avatar answered Sep 23 '22 14:09

D Stanley


What does invalid mean to you? That's defined by what your code can deal with. A Guid is just 16 arbitrary bytes without any mandatory format. You define what kinds of Guid you want to accept.

In that sense any Guid object is valid by itself. There's no invalid state for this struct.

It is like an int. Any int is valid.

like image 44
usr Avatar answered Sep 24 '22 14:09

usr