Specified object to convert the Guid. - CSharp System

CSharp examples for System:Converter

Description

Specified object to convert the Guid.

Demo Code


using System.Xml.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System.IO;/*from w ww. j a  va  2s  . c om*/
using System.Globalization;
using System.Collections.Generic;
using System;

public class Main{
        /// <summary>
        /// Specified object to convert the Guid.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Guid? ToGuid(Object value)
        {
            if (value == null)
            {
                return null;
            }

            Guid guid;
            if (Guid.TryParse(value.ToString(), out guid))
            {
                return guid;
            }
            return null;
        }
        /// <summary>
        /// Specified object to convert the Guid.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static Guid ToGuid(Object value, Guid defaultValue)
        {
            var guid = ToGuid(value);
            return guid.HasValue ? guid.Value : defaultValue;
        }

}

Related Tutorials