开发者

How to Initialize Values to a HashSet<String[,]> in C#

I am using VS 2008 and I need to know how to initialize the HashSet. I know Some values which is needed to add it during initialization. How can I add values to the 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 E开发者_如何学Crror..

The ultimate aim is to prevent user from entering duplicate values.I need to check it from different forms and from different tables and different fields.I go for querying the database using a dynamic query. I need to store the table name and column name in some index,value,value format for eg My tablename is tblCategory and field name is CatName.So I will store the value in the way0,tblCategory,CatName. So I will use Ajax to a handler page and in that I am using the above code.Here I am passing 0 to get the first value[tablename and column name],1 for another table and field and so on. So I thought of using this way.

Whether I am using the correct way or any other way to achieve the aim ie to prevent user from entering duplicate values ?

Thanks ,Harie


If you want to initialize the HashSet with a set of known values in one step, you can use code similar to the following:

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

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

This is called a collection initializer. It was introduced in C# 3.0, and includes the following elements:

  • A sequence of object initializers, enclosed by { and } tokens and separated by commas.
  • Element initializers, each of which specifies an element to be added to the collection object.


I want to write java code and assume that it is the same as in 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"};

or

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};


To initialize and assign values to a HashSet in one line, we can do something like this:

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

In C#, this is considered using an object initializer and can be read about in MSFT docs: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-initialize-objects-by-using-an-object-initializer


tblNames.Add(new [,] { { "0", "tblAssetCategory" }});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜