ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

IntToGuid

Converts an integer to a Guid. This could be used within a unit test to mock objects.

Source

public static Guid ToGuid(this int value)
{
    byte[] bytes = new byte[16];
    BitConverter.GetBytes(value).CopyTo(bytes, 0);
    return new Guid(bytes);
}

Example

class Customer
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

public IEnumerable<Customer> GetCustomers()
{
    for (int i = 0; i < 10; i++)
    {
        yield return new Customer
        {
            Id = i.ToGuid(),
            Name = "Name " + i
        };
    }
}

Author: Tom De Wilde

Submitted on: 29 mei 2013

Language: C#

Type: System.Int32

Views: 5234