Tuesday, April 19, 2011

Taking Advantage of Debug class in .NET

Debug Class found in System.Diagnostics Nampespace is designed for debugging code in a effective manner, still very low percent of the developers utilize it. So I concieve of sharing the use of Debug class with developers as it was born to be utilized in Debugging.


using System.Collections.Generic;
using System.Diagnostics;
 
namespace DemoDebugClass
{
    public class Student
    {
        public int    Id   { get; set; }
        public string Name { get; set; }
    }
 
    public static class StudentExtensions
    {
        public static void PrintStudentBelowID25(this IEnumerable<Student> StudentList)
        {
            foreach (var student in StudentList)
            {
                Debug.WriteLine(string.Format("Student Name: {0}", student.Name));
                Debug.WriteLineIf(student.Id < 25, "Required Student Found!");
            }
        }
    }  
 
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> studentsList = new List<Student>();
            studentsList.Add(new Customer { Id = 1, Name = "Customer A" });
            studentsList.Add(new Customer { Id = 2, Name = "Customer B" });
            studentsList.Add(new Customer { Id = 3, Name = "Customer C" });
            studentsList.PrintStudentBelowID25();
        }
    }
}

No comments:

Post a Comment