Easy C# question about Guids

I am taking SharePoint dev courses about SP workflows and the assumption is that I have already mastered C#, which i have not, I only have a decent understanding of the principles. So the course keeps on pulling a rabbit out of a hat, so to speak, without explanation. So I might have many more questions like this ion the future, but hopefully they will all be easy points for everyone.

I understand what default(Guid) does after some research, and the course code used this in another exercise
public Guid varname = default(Guid);

But now in this exercise, when it is declaring a field to hold a Guid value it is:
public Guid workflowId = default(System.Guid);

At first I thought it needed System.Guid instead of just Guid because I have no using statement at the top for System.Guid, so in this case, it has to be explicit. So when I went to add using System.Guid, intellisense does not know what I am talking about. I have read that Guid is in the System namespace, so I thought that maybe I need to add a reference to System.Guid, but under .net, I cannot find any such reference.

So what's going on?

Also, inside of an event handler, it calls this
taskID = Guid.NewGuid();

I understand that the Guid has a static method called NewGuid, which does not create an empty guid like in the case of the field variable declaration above, but instead generates an actual Guid value. But I don't understand how this method could just seemingly be called out of nowhere right after a Guid value type. Does it have something to do with the fact that taskID has already been declared as a Guid field/variable?
LVL 9
BobHavertyComhAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
käµfm³d 👽Connect With a Mentor Commented:
Admittedly, I have no SP development experience. Based on what I read above:

At first I thought it needed System.Guid instead of just Guid because I have no using statement at the top for System.Guid, so in this case, it has to be explicit.
That is correct.

I have read that Guid is in the System namespace, so I thought that maybe I need to add a reference to System.Guid, but under .net, I cannot find any such reference.
Try mscorlib.dll.

But I don't understand how this method could just seemingly be called out of nowhere right after a Guid value type.
As long as you declared the variable somewhere before you attempt to use it (e.g. assign to it), then you can use it. So...

Does it have something to do with the fact that taskID has already been declared as a Guid field/variable?
...yes.
0
 
Kyle AbrahamsConnect With a Mentor Senior .Net DeveloperCommented:
You would need System.Guid if you didn't have a "using System;"  statement.  If you have the using statement, you should just be able to refer to Guid as you normally would.  The using refers to the namespace (eg: system) . . . and then you get access to all classes in that namespace by default.


Just a note on one of the most useful tricks I've ever learned.

Type this into your page (remove references to system.Collection if it's there):

        Thread x;

Click back on the word "Thread"

Hit ctrl + period

This will automatically ask you whether you want to add the using statement or change the list to be the fully qualified name . . . very useful if you're trying to find out where something is coming from.
0
 
BobHavertyComhAuthor Commented:
Thanks guys. It looks like it doesn't even need default(System.Guid) and default(Guid) seems to build fine, so maybe it's just a redundancy in the course's code as it's not really the best course in the world, but it's cheap. what they did was not wrong, it just wasn't necessary. As far as a using statement, apparently "using System;" itself takes care of it after what I have just read, even though it is in the mscorlib.dll. But when i go to add mscorlib.dll to the references, it tells me that this will be added at compile time, so it does not add it.

As far as Guid.NewGuid, the leading Guid threw me off, but I suppose that this is the same thing as saying String.Empty, so i can just look at it that way, where if you call a static method of a class, you have to refer to the class itself first.

Thank you much. I may have many more questions following, and hopefully they are all easy for anyone like this one was. Microsoft does technically have these explanations, but they aren't very explicit and while I can often figure the explanation out myself, I sometimes cannot and need some confirmation.
0
 
BobHavertyComhAuthor Commented:
Correct on one thing. The code was System.Guid because one instance of this was automatically populated by default by VA, and the other was generated by the auto field builder when one is binding something to a field, so i guess that any auto builders try to be as safe as possible and make no assumptions, so it is System.Guid rather than just Guid Either way, apparently it is the same thing.
0
 
käµfm³d 👽Commented:
As far as Guid.NewGuid, the leading Guid threw me off, but I suppose that this is the same thing as saying String.Empty, so i can just look at it that way, where if you call a static method of a class, you have to refer to the class itself first.
Correct.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.