Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Error..

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

like image 774
kbvishnu Avatar asked Jan 14 '11 05:01

kbvishnu


People also ask

How do you initialize a Set of values?

To initialize a Set with values, pass an iterable to the Set constructor. When an iterable is passed to the Set constructor, all elements get added to the new Set . The most common iterables to initialize a Set with are - array, string and another Set .

Can we sort HashSet in C#?

You can't.

How does HashSet work in C#?

A HashSet<T> object's capacity automatically increases as elements are added to the object. A HashSet<T> collection is not sorted and cannot contain duplicate elements. HashSet<T> provides many mathematical set operations, such as set addition (unions) and set subtraction.


1 Answers

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.
like image 178
Cody Gray Avatar answered Oct 11 '22 22:10

Cody Gray