admin管理员组

文章数量:1302374

Is ReadonlySet means set of ReadOnly properties as same as ReadOnlyCollection in C#? I am not able to find out any document for it from anywhere. Could you please let me know what is the use of ReadonlySet and How can we do implementation of it?

let readonly: Readonly<number> = 1;
let readonlySet: ReadonlySet<number> = ???// 

Is ReadonlySet means set of ReadOnly properties as same as ReadOnlyCollection in C#? I am not able to find out any document for it from anywhere. Could you please let me know what is the use of ReadonlySet and How can we do implementation of it?

let readonly: Readonly<number> = 1;
let readonlySet: ReadonlySet<number> = ???// 
Share edited Jul 26, 2019 at 19:08 Ian 6,1546 gold badges45 silver badges76 bronze badges asked Jan 18, 2018 at 15:45 Ramesh RajendranRamesh Rajendran 38.7k49 gold badges157 silver badges239 bronze badges 3
  • @T.J.Crowder I thought the function of ReadOnlySet is same as ReadOnlyCollection which is I have using in C#(backend). I don't get any document for this. – Ramesh Rajendran Commented Jan 18, 2018 at 15:50
  • (Yes, I saw the edit.) If it's in code you're using, you must be able to figure out what's providing it and, from there, find the documentation or source code. Without knowing what class you're talking about, it's hard to help you. In general: No, a set is not the same as a collection. A collection can contain the same value twice. A set cannot. – T.J. Crowder Commented Jan 18, 2018 at 15:51
  • @T.J.Crowder There have no documentation, then what will I do? Could you please let me know if you found any documentation about this? – Ramesh Rajendran Commented Jan 18, 2018 at 15:52
Add a ment  | 

1 Answer 1

Reset to default 12

ReadonlySet is a type similar to Set, but it does not have a add / delete method, so elements can only be added during construction:

  const regular: Set<number> = new Set([1,2,3]);
  regular.add(4);
  
  const readonly: ReadonlySet<number> = new Set([1,2,3]);
  readonly.add(4) //fails, add not a function

Reference

Unlike the readonlySet in C# you referenced, typescripts ReadonlySet is not actually a class/constructor. Its just a type. As typescript has duck typing, the Set is kind of a superclass to ReadonlySet, so the type can be "typecasted". This is actually only for typechecking, it has no runtime influence.At runtime, it's just a JavaScript Set.

本文标签: javascriptDetails About ReadonlySetStack Overflow