如何在C#中将值初始化为HashSet <String [,]>

kbv*_*hnu 9 c# initialization hashset

我正在使用VS 2008,我需要知道如何初始化HashSet.我知道在初始化期间添加它需要一些值.如何向tblNames添加值.

System.Collections.Generic.HashSet<String[,]> tblNames;
            tblNames = new System.Collections.Generic.HashSet<string[,]>();

tblNames.Add(new String[0,0] {"tblCategory","CatName" ,}); // this is showing Error..
Run Code Online (Sandbox Code Playgroud)

最终目的是防止用户输入重复值.我需要从不同的表格和不同的表和不同的字段中检查它.我使用动态查询来查询数据库.我需要以一些索引,值,值格式存储表名和列名for eg My tablename is tblCategory and field name is CatName.所以我将存储该值的方式0,tblCategory,CatName.所以我将Ajax用于处理程序页面,并且我正在使用上面的代码.我正在传递0获取first value[tablename and column name],1用于另一个表和字段等等.所以我想用这种方式.

我是使用正确的方式还是以任何其他方式实现目标,即阻止用户输入重复值?

谢谢,哈里

Cod*_*ray 13

如果要HashSet在一个步骤中使用一组已知值初始化,可以使用类似于以下内容的代码:

HashSet<string[,]> tblNames;
string[,] stringOne = new string[1, 1];
string[,] stringTwo = new string[1, 1];

tblNames = new HashSet<string[,]> { stringOne, stringTwo };
Run Code Online (Sandbox Code Playgroud)

这称为集合初始值设定项.它是在C#3.0中引入的,包括以下元素:

  • 对象初始化,由封闭的序列{}标记和用逗号分开.
  • 元素初始值设定项,每个元素初始值设定项指定要添加到集合对象的元素.


Mat*_* C. 7

要在一行中初始化 HashSet 并将值分配给 HashSet,我们可以执行以下操作:

var set = new HashSet<string>() { "some value1", "some value2" };

在 C# 中,这被认为是使用对象初始值设定项,并且可以在 MSFT 文档中阅读:https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to -使用对象初始化器初始化对象


小智 6

我想编写Java代码并假定它与C#中的相同

HashSet<T> tblNames = new HashSet<T>(); // T should be same

HashSet<string> tblNames = new HashSet<string> ();
tblNames.add("a");
tblNames.add("b");
tblNames.add("c");

or simply
HashSet<string> tblNames = new HashSet<string> {"a", "b", "c"};

HashSet<String[,]> tblNames = new HashSet<String[,]> (); // same logic you can add array here
tblNames.add(stringArray1);
tblNames.add(stringArray2);

or again
HashSet<String[,]> tblNames = new HashSet<String[,]> {stringArray1, strginArray2};
Run Code Online (Sandbox Code Playgroud)


web*_*rph 4

tblNames.Add(new [,] { { "0", "tblAssetCategory" }});
Run Code Online (Sandbox Code Playgroud)