Saturday, March 20, 2010

S.O.L.I.D C# Code - Writing less code to carry out an activity unlike conventional methods (Comparison)

Hey All!

I am back w/ a C# post at last! Been while since I posted anything related to C#. Its 1'o clock, so I may have to edit this post later. I was reading some posts about S.O.L.I.D C# principles and I thought that I should post something about that. We may be (are) unknowingly using a lot of S.O.L.I.D principles in our day-to-day life probably.

Check out the following example. You would be able to notice clearly that S.O.L.I.D code uses much lesser amount of code to accomplish the same task that conventional code does. Keep in mind that List<T> implements the IEnumerable<T> and IEnumerable interface, but, ArrayList only implements IEnumerable. And, to say the least, both List<T> and ArrayList are extremely useful data structures (I feel!).

public class Content
{
public int a;
public string b;
}

public class EnumQu
{
public ArrayList arrList = null;
public List<Content> list = null;

private static void print(string message)
{
Console.WriteLine("");
Console.WriteLine(message);
Console.WriteLine("");
}

public void loadData()
{
arrList = new ArrayList();
list = new List<Content>();

for (int i = 0; i < 10; i++)
{
Content c = new Content();
c.a = (i + 1);
c.b = "String " + (i + 1);

arrList.Add(c);
list.Add(c);
}
}

public void arrayListTests()
{
// Display the items in ArrayList
// ArrayList implements IEnumerable but not IEnumerable<T>
// so .ForEach cannot be invoked

// S.O.L.I.D code to display (at least partially)
print("Select (S.O.L.I.D):");

var query = (from Content c in arrList select new { c });

foreach (var q in query) { Console.WriteLine(q.c.a + " - " + q.c.b); }

// S.O.L.I.D code to query for a value
print("Select - Single (S.O.L.I.D):");

var query_sel = (from Content c in arrList where c.a == 5 select new { c });

foreach (var q in query_sel) { Console.WriteLine(q.c.a + " - " + q.c.b); }

// Conventional C# code
print("Select:");

foreach (Content c in list)
{
Console.WriteLine(c.a + " - " + c.b);
}

print("Select - Single:");

foreach (Content c in list)
{
if (c.a == 5)
{
Console.WriteLine(c.a + " - " + c.b);
}
}
}

public void listTests()
{
// Display the items in List<T>
// List<T> implements IEnumerable<T> so .ForEach can be invoked

// S.O.L.I.D code
print("Select (S.O.L.I.D):");

list.ForEach(delegate(Content c){ Console.WriteLine(c.a + " - " + c.b); });

// S.O.L.I.D code to query for a value
print("Select - Single (S.O.L.I.D):");

List<Content> cList = list.Where(c => c.a == 5).ToList();

cList.ForEach(delegate(Content c){ Console.WriteLine(c.a + " - " + c.b); });

// Conventional C# code
print("Select:");

foreach (Content c in list)
{
Console.WriteLine(c.a + " - " + c.b);
}

print("Select - Single:");

foreach (Content c in list)
{
if (c.a == 5)
{
Console.WriteLine(c.a + " - " + c.b);
}
}
}

public static void Main(string[] args)
{
EnumQu enumQu = new EnumQu();
enumQu.loadData();
enumQu.arrayListTests();
enumQu.listTests();
}
}

As said before, I may have to add more information in here!! See you soon with another post!!!

Happy coding!

~ Karthik

No comments: