作为c#工程师,set⾥的元素是不能重复的,那么⽤什么⽅法来区分重复与否呢? 是⽤==还是equals()? 它们有 何区别?


在C#中,对于`Set`集合,其内部实现会确保元素是唯一的。如果你试图向`Set`添加一个已经存在的元素,那么`Add`方法会返回`false`,而不会改变`Set`的内容。你不需要手动去检查一个元素是否已经存在于`Set`中。 如果你真的需要检查一个元素是否已经存在于`Set`中,你可以使用`Contains`方法。这个方法会返回一个布尔值,告诉你元素是否已经存在于`Set`中。 至于你提到的`==`和`Equals()`方法,它们在比较对象时有些不同。 * `==`运算符在比较两个对象时,会检查它们是否在内存中是同一个对象,也就是说它们指向的是否是同一个地址。 * `Equals()`方法在比较两个对象时,会检查它们是否是相同的类型,并且它们的所有属性是否都相同。对于引用类型,如果它们引用的是同一个对象,那么`Equals()`也会返回`true`。 在大多数情况下,对于值类型(比如整型、浮点型、字符串等),你可以安全地使用`==`和`Equals()`来比较它们是否相等。对于引用类型,你需要根据你的需求来决定使用哪一个。如果你关心的是两个对象是否在内存中是同一个对象,那么你应该使用`==`运算符。如果你关心的是两个对象的内容是否相同,那么你应该使用`Equals()`方法。 [2023-11-09 17:55:21 | AI写代码神器 | 305点数解答]
相关提问
- 作为c#工程师,set⾥的元素是不能重复的,那么⽤什么⽅法来区分重复与否呢? 是⽤==还是equals()? 它们有 何区别?(305点数解答 | 2023-11-09 17:55:21)239
- 作为c#工程师,整数list中取出最⼤数(找最⼤值)不能⽤max⽅法?(448点数解答 | 2023-11-09 17:55:36)261
- use [smarttagsystem] go /****** object: storedprocedure [dbo].[pr_createdtables] script date: 2024/8/29 星期四 15:33:01 ******/ set ansi_nulls on go set quoted_identifier on go alter procedure [dbo].[pr_createdtables] @table_name_sign varchar(50) --课程标识 as declare @sql_answer nvarchar(max) --答案表创建sql declare @sql_classify nvarchar(max) --章节考点表 declare @sql_exam nvarchar(max)--试卷表 declare @sql_problem nvarchar(max)--题目表 declare @sql_problemtype nvarchar(max)--题目类型表 set @sql_an(33点数解答 | 2024-08-29 15:34:33)225
- create table `py_plan_opportunity_point` ( `id` bigint not null auto_increment comment '主键id', `location_code` varchar(32) character set utf8mb4 collate utf8mb4_0900_ai_ci not null comment '规划点/机会点编码', `location_name` varchar(20) character set utf8mb4 collate utf8mb4_0900_ai_ci default null comment '规划点名称', `point_type` char(2) character set utf8mb4 collate utf8mb4_0900_ai_ci default null comment '点类型(1:规划点/2:机会点)', primary key (`id`) using **ree, key `py_plan_opportunity_point_uniq(328点数解答 | 2024-09-04 16:07:48)221
- create table `py_plan_opportunity_point` ( `id` bigint not null auto_increment comment '主键id', `location_code` varchar(32) character set utf8mb4 collate utf8mb4_0900_ai_ci not null comment '规划点/机会点编码', `location_name` varchar(20) character set utf8mb4 collate utf8mb4_0900_ai_ci default null comment '规划点名称', `point_type` char(2) character set utf8mb4 collate utf8mb4_0900_ai_ci default null comment '点类型(1:规划点/2:机会点)', primary key (`id`), unique key `py_plan_opportunity_point_uniq` (`location_code(188点数解答 | 2024-09-04 16:08:40)338
- create table `py_plan_opportunity_point` ( `id` bigint not null auto_increment comment '主键id', `location_code` varchar(32) character set utf8mb4 collate utf8mb4_0900_ai_ci not null comment '规划点/机会点编码', `location_name` varchar(20) character set utf8mb4 collate utf8mb4_0900_ai_ci default null comment '规划点名称', `point_type` char(2) character set utf8mb4 collate utf8mb4_0900_ai_ci default null comment '点类型(1:规划点/2:机会点)', primary key (`id`), unique key `py_plan_opportunity_point_uniq` (`location_code`(348点数解答 | 2024-09-04 16:09:36)189
- if (@code2 like 'item_ch_' + @itemcodeprefix + '_a_rare' and @optlevel >= 1 - @ck and @cl1 = 24670 and @cl2 = 24669 and @clsl1 > 1 and @clsl2 > 1) begin select @charname = charname16 from _char where charid = @charid; update _inventory set itemid=0 where charid=@charid and slot=13 update sro_vt_shard.._items set data=data-1 where id64 in (select itemid from sro_vt_shard.._inventory where slot=14and charid=@charid) update sro_vt_shard.._items set data=data-1 where id64 in ((51点数解答 | 2024-10-22 11:24:35)183
- if (@code2 like 'item_ch_' + @itemcodeprefix + '_a_rare' and @optlevel >= 1 - @ck and @cl1 = 24670 and @cl2 = 24669 and @clsl1 > 1 and @clsl2 > 1) begin select @charname = charname16 from _char where charid = @charid; update _inventory set itemid=0 where charid=@charid and slot=13 update sro_vt_shard.._items set data=data-1 where id64 in (select itemid from sro_vt_shard.._inventory where slot=14and charid=@charid) update sro_vt_shard.._items set data=data-1 where id64 in (select itemid from sro_(969点数解答 | 2024-10-22 11:26:02)237
- c++ 【题目描述】 我们中国人对老鼠的感情可不一般,鼠是中国传统十二生肖之首。 那么 2020 年出生的“20 后”是否都是“鼠宝宝"呢?其实不是,2020 年 1 月 1 日 ~ 1 月 24 日出生的“20 后”,仍然是“猪宝宝",因为他们出生在农历己亥猪年;大年初一(1 月 25 日)及之后出生的“20 后”才是“鼠宝宝”。那么接下来请你判断一下,以下生日的宝宝是“猪宝宝”还是“鼠宝宝”? 【输入】 符合常识的两个空格分隔的整数 month ,day ,分别代表宝宝出生的月份及日子。(1≤month≤12,1≤day≤31 ) 【输出】 若是“猪宝宝”请输出 "Pig";若是“鼠宝宝”请输出 "Mouse"。 【输入样例】 1 1 【输出样例】 Pig(343点数解答 | 2025-02-22 17:12:21)103
- 作为c#工程师,在.net(c# or vb.net)中,appplication.exit 还是 form.close有什么不同?(332点数解答 | 2023-11-09 17:47:25)194
- 动态操作 dom 元素 要求:使用 jquery 创建一个待办事项列表应用程序,包含一个文本框和“添加”按钮。用户可以输入任务 并点击“添加”按钮将其添加到列表中。还需要为每个任务添加“删除”按钮,点击后可以删除对应的任务。(1015点数解答 | 2024-10-26 23:41:22)306
- 动态操作 dom 元素 要求:使用 jquery 创建一个待办事项列表应用程序,包含一个文本框和“添加”按钮。用户可以输入任务并点击“添加”按钮将其添加到列表中。还需要为每个任务添加“删除”按钮,点击后可以删除对应的任务。(1139点数解答 | 2024-10-29 08:44:48)231