A better approach for using lists in C#

In the following article we are going to learn ways of using List in C#. I will be discussing anonymous methods feature of C# 2.0 within List extension methods like Findall(),Foreach() and Sort(). I found this necessary to be shared amongst developer’s community since I seldom find developers using inbuilt methods for manipulating a List.

Before jumping on the usage I would like to highlight that “Anonymous methods were introduced in C# 2.0 and in C# 3.0 and later, lambda expressions superseded anonymous methods .There is one case in which an anonymous method provides functionality not found in lambda expressions. Anonymous methods enable you to omit the parameter list. This means that an anonymous method can be converted to delegates with a variety of signatures. This is not possible with lambda expressions – MSDN

Starting off with my discussion I have created a Radio button list that will be holding name value pairs.

public class RadioButtonlist
{
public string Name { get; set; }
public long Value { get; set; }

public RadioButtonlist(string a, long v)
{
Name = a;
Value = v;
}
}

Next we are going to populate this list with custom data. For this example we assume that radio button list contains a list of Products from Apple and Samsung brand.

 

List<RadioButtonlist> rb = new List<RadioButtonlist>()
{
new RadioButtonlist("Apple", 0000000000001) ,
new RadioButtonlist("Apple", 0000000000022),
new RadioButtonlist("Samsung", 0000000001233)
};

 

Here am going to show you the use of inbuilt list methods.

Finding Apple products using FindAll() on our RadioButtonlist list

List<RadioButtonlist> apple = rb.FindAll(delegate (RadioButtonlist r) { return r.Name == "Apple"; });

Next I am listing all Apple products using Foreach() on our apple list

 

apple.ForEach(delegate (RadioButtonlist r)
{
Console.WriteLine("Brand: {0}", r.Name);
});

 

In order to sort the products I will simply be using Sort() method within our List

apple.Sort(delegate (RadioButtonlist r1, RadioButtonlist r2)
{
return r1.Value.CompareTo(r2.Value); // here I am sorting by Values
});

The above methods produces results same like methods mentioned underneath but with a smart approach.

 

List<RadioButtonlist> rb = new List<RadioButtonlist>()
{
new RadioButtonlist("Apple", 0000000000001) ,
new RadioButtonlist("Apple", 0000000000002),
new RadioButtonlist("Samsung", 0000000000003)
};

List<RadioButtonlist> apple = new List<RadioButtonlist>();

foreach (RadioButtonlist apple2 in rb)
{
if (apple2.Name == "Apple")
apple.Add(new RadioButtonlist(apple2.Name, apple2.Value));
}

foreach (RadioButtonlist apple2 in apple)
{
Console.WriteLine("Brand: {0}", apple2.Name);
}

ProductComparerByProductID ac = new ProductComparerByProductID();
apple.Sort(ac);

public class ProductComparerByProductID : IComparer<RadioButtonlist>
{
public int Compare(RadioButtonlist x, RadioButtonlist y)
{
if (x.Value > y.Value) return 1;
if (x.Value < y.Value) return -1;
return 0;
}
}

 

I hope that by now you are able to understand the usage of methods associated with C# list class. Thanks for reading!

 

Advertisements

SOLID Design Principle

In this article I am going to talk about the SOLID principles which are a refinement of object-oriented design principles, consisting of five design principles formulated by Robert Martin.

“SOLID” where ‘S’ stands for Single Responsibility, ‘O’ for Open/Closed Principle, ‘L’ for Liskov’s Principle, ‘I’ for Interface Segregation and lastly ‘D’ for Dependency Inversion.
Read More »