Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » Learn .NET » GUIDs in C# and .NET

GUIDs in C# and .NET

This article gives a basic understanding of GUIDs in .NET Framework.

Page Views : 16560
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


GUID stands for Global Unique Identifier.

A GUID is a 128-bit integer (16 bytes) that you can use across all computers and networks wherever a unique identifier is required.


Here are some frequently asked questions about GUIDs.


A. How many GUIDs in Microsoft Windows can one computer generate without rolling over or running out?

Answer: Without getting into detail, let me tell you there are 2^122 or 5,316,911,983,139,663,491,615,228,241,121,400,000 possible combination.

Reason is beyond the scope of this article.


B. Which class in .NET Framework is used to generate Guid?

Answer: System.GUID class represents a GUID in .NET Framework.


C. How can we generate GUID with SQL Server?

Answer: We can generate GUID in Sql Server with the help of NEWID() function

D. Which namespace must be referenced to utilize the GUID attribute. ?

Answer:  System.Runtime.InteropServices namespace must be referenced to utilize the GUID attribute.

This can be done as shown in following code:-


using System;

using System.Runtime.InteropServices;

namespace TESTGUID {
[Guid("9245fe4a-d402-451c-b9ed-9c1a04247482")]

class Example {
//Your Implementation
                           }           
                      }


Generating GUID in .NET Framework using C#

We can generate GUID by calling following code snippet. As you can see from this code, we use Guid.NewGuid() method to generate a new GUID in C#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{

    class Program
    {
        static int Main(string[] args)
        {
                Guid obj = Guid.NewGuid();                          

                Console.WriteLine("New Guid is " + obj.ToString());
                Console.ReadLine();
            return -1;
        }
    }
}

Note that above in above code we have used the NewGuid Method which will create new GUID .

A common mistake done by C# developers is to create an object of GUID and trying to print that.

The following code snippet reflects that mistake.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static int Main(string[] args)
        {
                Guid obj = new Guid();
Console.WriteLine("New Guid is " + obj.ToString());
                Console.ReadLine();
                return -1;
        }
    }
}


The output of above console program will be always be 16 byte with all 0 .

Everytime same thing will come.

So while generating GUID use NewGuid Method.


Also note that Guid only contains alphanumeric characters and none of non-alphanumeric character will be seen in GUID except "-";

Hope it helps.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Prasoon

I am presently working at Inkrea Consulting Pvt ltd for 5 months. Prior to that, I was working at Accenture-Bangalore for more than two and half years. My Academic background includes bachelor of technology degree in Computer Science from National Institute of Technology, Jalandhar. 

My hobbies includes reading Novels and watching cricket.

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.