It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
foreach (var item in HashSet<T>)
{
}
What should be the var?
Your code doesn't compile, you should use something like:
HashSet<int> hashSet = new HashSet<int>(); //create a HashSet of integers
//populate hashSet
foreach(var value in hashSet){ //where var is of type int
//...
}
var (implicitly typed variable) will be exactly the type you have defined in T when you declared your HashSet.
So for example, if you declared it HashSet<int>, var will be of type int.
var is T (30 characters) (30 characters) (30 characters) (30 characters)
var is T, float the mouse over var and VS will tell you the type.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a string variable which has the value that the user taps. I need to make another variable which its name will be the string's value.
How do / can I do that?
No, you cannot do that: the closest you can get is a Dictionary<string,object> (you can replace the object with some other type). Using this dictionary you would be able to create associations between strings (known as "keys") and values stored in the dictionary.
IDictionary<string,object> variables = new Dictionary<string,object>();
string varName = "hello";
variables[varName] = "world";
Console.WriteLine("Name: {0} Value: {1}", varName, variables[varName]);
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am looking for something like PHP's associative arrays that supports nesting too. For instance, I am creating a Dictionary Object like following:
System.Collections.Specialized.OrderedDictionary userRoles = new System.Collections.Specialized.OrderedDictionary();
userRoles["UserId"] = "120202";
userRoles["UserName"] = "Jhon Doe";
// 2D array Like
userRoles["UserRoles"][0] = "CAN_EDIT_LIST";
userRoles["UserRoles"][1] = "CAN_EDIT_PAGE";
Then I would like to access them by KeyNames instead of index values. Is it possible?
OrderedDictionary uses objects for both keys and values.
To achieve nesting, just set the value to be another dictionary:
userRoles["UserRoles"] = new Dictionary();
Then you can use:
((Dictionary())userRoles["UserRoles"])["MyKey"] = "My Value";
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How having an array of objects with property "name" get array of strings (with names corresponding to each object)?
Assuming that you want to select a specific property from each object in an array. If that property is called Name and is of type string, then you can do the following:
IEnumerable<string> names = namedObjects.Select(x => x.Name);
ObjectWithNameString[] objects = ...?
string[] names = new string[objects.Length];
for (int i = 0; i < objects.Length; i++)
{
names[i] = objects[i].Name;
}
For a better answer expand and clarify the question.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
i used this code:
List<string> lists=new List<string>("apple","orange","banana","apple","mang0","orange");
string names;
names=lists.Distinct()
is that correct?
No, the variable names has to be a collection. The Distinct method returns an enumerator, so you probably want to enumerate the result and realise it as a list:
List<string> names = lists.Distinct().ToList();
You can sort the list, then check two and two:
list.Sort();
Int32 index = 0;
while (index < list.Count - 1)
{
if (list[index] == list[index + 1])
list.RemoveAt(index);
else
index++;
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a list and a column; the column might have many values, and each value's length may vary.
If the length exceeds 100, I want to append /n at the end of it.
Need your help.
I give it a try, though a lot of questions are open (what type of list, what a column, ...).
In short: you can use String.Insert to insert text at a specified postition.
Assuming you have a List<Foo>, the class Foo has a string property Value (your column). If it's Length exceeds 100 the line should be wrapped:
foreach(Foo foo in foos)
{
if(foo.Value.Length > 100)
foo.Value = foo.Value.Insert(100, Environment.NewLine);
}
http://msdn.microsoft.com/en-us/library/system.string.insert.aspx
Here's running code with sample data.
http://ideone.com/sUKLk