本文整理匯總了C#中System.HashCode結構體的典型用法代碼示例。如果您正苦於以下問題:C# HashCode結構體的具體用法?C# HashCode怎麽用?C# HashCode使用的例子?那麽, 這裏精選的結構體代碼示例或許可以為您提供幫助。
HashCode結構體屬於System命名空間,在下文中一共展示了HashCode結構體的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: OrderOrderLine
//引入命名空間
using System;
using System.Collections.Generic;
public struct OrderOrderLine : IEquatable<OrderOrderLine>
{
public int OrderId { get; }
public int OrderLineId { get; }
public OrderOrderLine(int orderId, int orderLineId) => (OrderId, OrderLineId) = (orderId, orderLineId);
public override bool Equals(object obj) => obj is OrderOrderLine o && Equals(o);
public bool Equals(OrderOrderLine other) => OrderId == other.OrderId && OrderLineId == other.OrderLineId;
public override int GetHashCode() => HashCode.Combine(OrderId, OrderLineId);
}
class Program
{
static void Main(string[] args)
{
var set = new HashSet<OrderOrderLine>
{
new OrderOrderLine(1, 1),
new OrderOrderLine(1, 1),
new OrderOrderLine(1, 2)
};
Console.WriteLine($"Item count: {set.Count}.");
}
}
輸出:
Item count: 2.
示例2: Path
//引入命名空間
using System;
using System.Collections.Generic;
public struct Path : IEquatable<Path>
{
public IReadOnlyList<string> Segments { get; }
public Path(params string[] segments) => Segments = segments;
public override bool Equals(object obj) => obj is Path o && Equals(o);
public bool Equals(Path other)
{
if (ReferenceEquals(Segments, other.Segments)) return true;
if (Segments is null || other.Segments is null) return false;
if (Segments.Count != other.Segments.Count) return false;
for (var i = 0; i < Segments.Count; i++)
{
if (!string.Equals(Segments[i], other.Segments[i]))
return false;
}
return true;
}
public override int GetHashCode()
{
var hash = new HashCode();
for (var i = 0; i < Segments?.Count; i++)
hash.Add(Segments[i]);
return hash.ToHashCode();
}
}
class Program
{
static void Main(string[] args)
{
var set = new HashSet<Path>
{
new Path("C:", "tmp", "file.txt"),
new Path("C:", "tmp", "file.txt"),
new Path("C:", "tmp", "file.tmp")
};
Console.WriteLine($"Item count: {set.Count}.");
}
}
輸出:
Item count: 2.
示例3: Path
//引入命名空間
using System;
using System.Collections.Generic;
public struct Path : IEquatable<Path>
{
public IReadOnlyList<string> Segments { get; }
public Path(params string[] segments) => Segments = segments;
public override bool Equals(object obj) => obj is Path o && Equals(o);
public bool Equals(Path other)
{
if (ReferenceEquals(Segments, other.Segments)) return true;
if (Segments is null || other.Segments is null) return false;
if (Segments.Count != other.Segments.Count) return false;
for (var i = 0; i < Segments.Count; i++)
{
if (!string.Equals(Segments[i], other.Segments[i], StringComparison.OrdinalIgnoreCase))
return false;
}
return true;
}
public override int GetHashCode()
{
var hash = new HashCode();
for (var i = 0; i < Segments?.Count; i++)
hash.Add(Segments[i], StringComparer.OrdinalIgnoreCase);
return hash.ToHashCode();
}
}
class Program
{
static void Main(string[] args)
{
var set = new HashSet<Path>
{
new Path("C:", "tmp", "file.txt"),
new Path("C:", "TMP", "file.txt"),
new Path("C:", "tmp", "FILE.TXT")
};
Console.WriteLine($"Item count: {set.Count}.");
}
}
輸出:
Item count: 1.
示例4: Path
//引入命名空間
using System;
using System.Collections.Generic;
public struct Path : IEquatable<Path>
{
public IReadOnlyList<string> Segments { get; }
public Path(params string[] segments) => Segments = segments;
public override bool Equals(object obj) => obj is Path o && Equals(o);
public bool Equals(Path other)
{
if (ReferenceEquals(Segments, other.Segments)) return true;
if (Segments is null || other.Segments is null) return false;
if (Segments.Count != other.Segments.Count) return false;
for (var i = 0; i < Segments.Count; i++)
{
if (!PlatformUtils.PathEquals(Segments[i], other.Segments[i]))
return false;
}
return true;
}
public override int GetHashCode()
{
var hash = new HashCode();
for (var i = 0; i < Segments?.Count; i++)
PlatformUtils.AddPath(ref hash, Segments[i]);
return hash.ToHashCode();
}
}
internal static class PlatformUtils
{
public static bool PathEquals(string a, string b) => string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
public static void AddPath(ref HashCode hash, string path) => hash.Add(path, StringComparer.OrdinalIgnoreCase);
}
class Program
{
static void Main(string[] args)
{
var set = new HashSet<Path>
{
new Path("C:", "tmp", "file.txt"),
new Path("C:", "TMP", "file.txt"),
new Path("C:", "tmp", "FILE.TXT")
};
Console.WriteLine($"Item count: {set.Count}.");
}
}
輸出:
Item count: 1.