Preferred style
-
I have written the following
List UserProjects = ...
string domain = cbxDomain.SelectedValue.ToString();
HashSet itemsToShow = new HashSet(); // See annotation 0And I was wondering which of the two snippets below people prefer A) Use a Predicate
foreach (Project p in UserProjects.FindAll(delegate(Project x){return x.DomainName.Equals(domain);}))
{
itemsToShow.Add(p.ProjectName);
}Or B) Nested if
foreach (Project p in UserProjects)
{
if (p.DomainName.Equals(domain))
{
itemsToShow.Add(p.ProjectName);
}
}0If you haven't used
HashSet
before, it is basically aList
that sort of behaves like aDictionary
in that doesn't allow duplicates, but doesn't throw anexception
if you try and add a duplicate. (I know that's an over simplification, - see MSDN[^] for full description) -
I have written the following
List UserProjects = ...
string domain = cbxDomain.SelectedValue.ToString();
HashSet itemsToShow = new HashSet(); // See annotation 0And I was wondering which of the two snippets below people prefer A) Use a Predicate
foreach (Project p in UserProjects.FindAll(delegate(Project x){return x.DomainName.Equals(domain);}))
{
itemsToShow.Add(p.ProjectName);
}Or B) Nested if
foreach (Project p in UserProjects)
{
if (p.DomainName.Equals(domain))
{
itemsToShow.Add(p.ProjectName);
}
}0If you haven't used
HashSet
before, it is basically aList
that sort of behaves like aDictionary
in that doesn't allow duplicates, but doesn't throw anexception
if you try and add a duplicate. (I know that's an over simplification, - see MSDN[^] for full description) -
I have written the following
List UserProjects = ...
string domain = cbxDomain.SelectedValue.ToString();
HashSet itemsToShow = new HashSet(); // See annotation 0And I was wondering which of the two snippets below people prefer A) Use a Predicate
foreach (Project p in UserProjects.FindAll(delegate(Project x){return x.DomainName.Equals(domain);}))
{
itemsToShow.Add(p.ProjectName);
}Or B) Nested if
foreach (Project p in UserProjects)
{
if (p.DomainName.Equals(domain))
{
itemsToShow.Add(p.ProjectName);
}
}0If you haven't used
HashSet
before, it is basically aList
that sort of behaves like aDictionary
in that doesn't allow duplicates, but doesn't throw anexception
if you try and add a duplicate. (I know that's an over simplification, - see MSDN[^] for full description)I personally would go with the second choice for the sake of readability.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
-
I have written the following
List UserProjects = ...
string domain = cbxDomain.SelectedValue.ToString();
HashSet itemsToShow = new HashSet(); // See annotation 0And I was wondering which of the two snippets below people prefer A) Use a Predicate
foreach (Project p in UserProjects.FindAll(delegate(Project x){return x.DomainName.Equals(domain);}))
{
itemsToShow.Add(p.ProjectName);
}Or B) Nested if
foreach (Project p in UserProjects)
{
if (p.DomainName.Equals(domain))
{
itemsToShow.Add(p.ProjectName);
}
}0If you haven't used
HashSet
before, it is basically aList
that sort of behaves like aDictionary
in that doesn't allow duplicates, but doesn't throw anexception
if you try and add a duplicate. (I know that's an over simplification, - see MSDN[^] for full description)The latter. But why are you usnig
.Equals
? -
I have written the following
List UserProjects = ...
string domain = cbxDomain.SelectedValue.ToString();
HashSet itemsToShow = new HashSet(); // See annotation 0And I was wondering which of the two snippets below people prefer A) Use a Predicate
foreach (Project p in UserProjects.FindAll(delegate(Project x){return x.DomainName.Equals(domain);}))
{
itemsToShow.Add(p.ProjectName);
}Or B) Nested if
foreach (Project p in UserProjects)
{
if (p.DomainName.Equals(domain))
{
itemsToShow.Add(p.ProjectName);
}
}0If you haven't used
HashSet
before, it is basically aList
that sort of behaves like aDictionary
in that doesn't allow duplicates, but doesn't throw anexception
if you try and add a duplicate. (I know that's an over simplification, - see MSDN[^] for full description)I'll join the crowd and prefer the latter for its readability; the former isn't bringing anything IMO. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
I have written the following
List UserProjects = ...
string domain = cbxDomain.SelectedValue.ToString();
HashSet itemsToShow = new HashSet(); // See annotation 0And I was wondering which of the two snippets below people prefer A) Use a Predicate
foreach (Project p in UserProjects.FindAll(delegate(Project x){return x.DomainName.Equals(domain);}))
{
itemsToShow.Add(p.ProjectName);
}Or B) Nested if
foreach (Project p in UserProjects)
{
if (p.DomainName.Equals(domain))
{
itemsToShow.Add(p.ProjectName);
}
}0If you haven't used
HashSet
before, it is basically aList
that sort of behaves like aDictionary
in that doesn't allow duplicates, but doesn't throw anexception
if you try and add a duplicate. (I know that's an over simplification, - see MSDN[^] for full description)why not just use: if (!itemsToShow.Contains(p.ProjectName)) itemsToShow.Add(p.ProjectName); gets rid of the foreach and the ugly LINQ statement. Please stop using LINQ for every little thing... the performance hit you take is big :). People who use LINQ all over there code often find themselves re-writting slow chunks down the road.
-
why not just use: if (!itemsToShow.Contains(p.ProjectName)) itemsToShow.Add(p.ProjectName); gets rid of the foreach and the ugly LINQ statement. Please stop using LINQ for every little thing... the performance hit you take is big :). People who use LINQ all over there code often find themselves re-writting slow chunks down the road.
-
While the second part is true,
SledgeHammer01 wrote:
why not just use: [something]
Because it does something entirely different.
Er, you're right... I just glossed over the example :).
-
I have written the following
List UserProjects = ...
string domain = cbxDomain.SelectedValue.ToString();
HashSet itemsToShow = new HashSet(); // See annotation 0And I was wondering which of the two snippets below people prefer A) Use a Predicate
foreach (Project p in UserProjects.FindAll(delegate(Project x){return x.DomainName.Equals(domain);}))
{
itemsToShow.Add(p.ProjectName);
}Or B) Nested if
foreach (Project p in UserProjects)
{
if (p.DomainName.Equals(domain))
{
itemsToShow.Add(p.ProjectName);
}
}0If you haven't used
HashSet
before, it is basically aList
that sort of behaves like aDictionary
in that doesn't allow duplicates, but doesn't throw anexception
if you try and add a duplicate. (I know that's an over simplification, - see MSDN[^] for full description)Option A should really be
foreach (Project p in UserProjects.FindAll(x => x.DomainName.Equals(domain)))
{
itemsToShow.Add(p.ProjectName);
}But I still think B is clearer. Also, why are you using .Equals instead of ==? For almost every class they are equivalent (even if I override Equals for value semantics I also override ==/!= because it's just too confusing otherwise) and == is prettier.
-
I have written the following
List UserProjects = ...
string domain = cbxDomain.SelectedValue.ToString();
HashSet itemsToShow = new HashSet(); // See annotation 0And I was wondering which of the two snippets below people prefer A) Use a Predicate
foreach (Project p in UserProjects.FindAll(delegate(Project x){return x.DomainName.Equals(domain);}))
{
itemsToShow.Add(p.ProjectName);
}Or B) Nested if
foreach (Project p in UserProjects)
{
if (p.DomainName.Equals(domain))
{
itemsToShow.Add(p.ProjectName);
}
}0If you haven't used
HashSet
before, it is basically aList
that sort of behaves like aDictionary
in that doesn't allow duplicates, but doesn't throw anexception
if you try and add a duplicate. (I know that's an over simplification, - see MSDN[^] for full description)