C# 集合(四) —— Set类

总目录
C# 语法总目录

集合四 Set

1. Set

有 HashSet 和 SortedSet,

  • 它们都不包含重复元素
  • 忽略添加重复值的请求
  • 无法根据位置访问元素
  • 使用Contains方法均使用散列查找,所以速度快

SortedSet 按照一定顺序保存元素,使用红黑树实现,而HashSet是根据Hash值保存元素和查找元素。

HashSet<int> evenNumbers = new HashSet<int>();
HashSet<int> oddNumbers = new HashSet<int>();

for (int i = 0; i < 5; i++)
{
    // Populate numbers with just even numbers.
    evenNumbers.Add(i * 2);

    // Populate oddNumbers with just odd numbers.
    oddNumbers.Add((i * 2) + 1);
}

foreach (var item in evenNumbers)
{
    Console.WriteLine(item);
}
Console.WriteLine("--------------------------");

foreach (var item in oddNumbers)
{
    Console.WriteLine(item);
}
Console.WriteLine("--------------------------");
HashSet<int> numbers = new HashSet<int>(evenNumbers);
foreach (var item in numbers)
{
    Console.WriteLine(item);
}
Console.WriteLine("--------------------------");
numbers.UnionWith(oddNumbers);

foreach (var item in numbers)
{
    Console.WriteLine(item);
}
/*
2
4
6
8
--------------------------
1
3
5
7
9
--------------------------
0
2
4
6
8
--------------------------
0
2
4
6
8
1
3
5
7
9
*/

总目录
C# 语法总目录

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
原型模式是一种创建型设计模式,它允许你通过复制一个已经存在的对象来创建新的对象,而不是通过实例化来创建。这种方式可以大大减少对象的创建时间和内存消耗。 在C#中,原型模式的实现需要满足两个条件: 1. 实现ICloneable接口,该接口只有一个方法Clone(),用于复制对象。 2. 对象必须是可复制的,即必须是浅复制或深复制。 浅复制是指复制一个对象,但是不复制对象中的引用型成员变量,这意味着复制的对象和原始对象共享相同的引用型成员变量。深复制则是复制一个对象及其引用型成员变量,这意味着复制的对象和原始对象不共享相同的引用型成员变量。 下面是一个使用原型模式的示例代码: ```csharp using System; namespace PrototypePattern { // 实现ICloneable接口 public class Person : ICloneable { public string Name { get; set; } public int Age { get; set; } public Address Address { get; set; } public object Clone() { // 深复制 return new Person { Name = this.Name, Age = this.Age, Address = new Address { Street = this.Address.Street, City = this.Address.City, State = this.Address.State } }; } public override string ToString() { return $"Name: {Name}, Age: {Age}, Address: {Address}"; } } public class Address { public string Street { get; set; } public string City { get; set; } public string State { get; set; } public override string ToString() { return $"{Street}, {City}, {State}"; } } class Program { static void Main(string[] args) { var person1 = new Person { Name = "Tom", Age = 20, Address = new Address { Street = "123 Main St", City = "New York", State = "NY" } }; // 使用Clone方法复制对象 var person2 = (Person)person1.Clone(); person2.Name = "Jerry"; person2.Address.Street = "456 Elm St"; Console.WriteLine(person1); Console.WriteLine(person2); } } } ``` 输出结果: ``` Name: Tom, Age: 20, Address: 123 Main St, New York, NY Name: Jerry, Age: 20, Address: 456 Elm St, New York, NY ``` 可以看到,使用原型模式可以方便地创建新的对象,而且不需要关心对象的创建过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值