Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • C# .Net : How to convert string to Guid ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 57.3k
    Comment on it

    C# .Net : How to convert string to Guid ?

    Guid represents a globally unique identifier. Guid in .Net framework is identified by System.GUID class.

    Program to generate a guid in .Net Framework using C#.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace GuidGeneration
    {
        class Program
        {
            static void Main(string[] args)
            {
                Guid guidobj = Guid.NewGuid();  // Generates a unique identifier
                Console.WriteLine("Guid generated is " + guidobj.ToString());
                Console.ReadLine();
            }     
        }
    }

    Output:

    Guid generated is 2bea3f36-3b97-4b1b-ba3b-50d87c465b89
    

    There are two methods to convert string representation into equivalent Guid

    1) Guid.Parse

    public static Guid Parse(){
    string input;
    }

    This method takes string as an input and returns it's equivalent Guid value. An ArgumentNullException is thrown when input parameter is null and FormatException is thrown when input parameter is in wrong format. 

     

    Program to convert string into guid in .Net Framework using C# Guid.Parse method.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace GuidGeneration
    {
        class Program
        {
            static void Main(string[] args)
            {
                Guid objGuid = Guid.Empty;
                string s = "AC761785-ED42-11CE-DACB-00BDD0057645";
                try
                {
                    objGuid = Guid.Parse(s);
                    Console.WriteLine("Guid generated is " + objGuid);
                    Console.ReadLine();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception:" + e);
                    Console.ReadLine();
                }
                       
            }     
        }
    }

    Output:

    Guid generated is ac761785-ed42-11ce-dacb-00bdd0057645
    

    2) Guid.TryParse

    public static bool TryParse(
    	string input,
    	out Guid result
    )

    This method contains two parameters string as an input and if this method returns true then result parameter contains a valid Guid and in case false is returned result parameter will contain Guid.Empty. 

     

    Program to convert string into guid in .Net Framework using C# Guid.TryParse method.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace GuidGeneration
    {
        class Program
        {
            static void Main(string[] args)
            {
                Guid objGuid = Guid.Empty;
                string s = "AC761785-ED42-11CE-DACB-00BDD0057645";
                if(Guid.TryParse(s,out objGuid))
                {
                    Console.WriteLine("Guid generated is " + objGuid);
                    Console.ReadLine();
                }
                else{
                    Console.WriteLine("The given string {0} cannot be converted to a guid", s);
                    Console.ReadLine();
    
                }          
            }     
        }
    }
    

    Output:

    Guid generated is ac761785-ed42-11ce-dacb-00bdd0057645

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: