TechSapphire Monday, 2024-12-09, 3:45 PM
Site menu
Login form
News
Play Games
  • Deep Freeze
  • Ice Slide
  • Gyroball
  • Fat Fish
  • Bush Royal Rampage
  • C# Best Practices

    In this article I have focused on practical aspect more, and also I have not included basic practices as you can learn them from any article available online. I have tried to cover basic practices which will minimize your coding effort as well as it will make your code more manageable and also will reduce run time exceptions. 

    Topics covered in today’s lecture are:

    • Common Coding Guidelines
    • SQL Helper Class
    • Using Lamda expression or LINQ
    • Some common tips
    • Cross resolution design
    • LINQ operators and operations

    Common Coding Guidelines

    Implementing IDisposable interface

    This interface let you to implement dispose method in your class, which is called automatically whenever object is not required, in that method you can close open database connections or can release any resource if it is not required. It make your code more optimized and also manageable as you are releasing resources at end of your object. Below is example of class implementing IDisposable interface:

    public partial class EmployeeWithIDisposable : IDisposable

        {

            public string name { get; set; }

            public string address { get; set; }

            public Employee obj = new Employee();

            public void Dispose()

            {

                //throw new NotImplementedException();

                this.address = string.Empty; ;

                this.name = string.Empty;

                this.obj = new Employee();

            }

        }

     

    After implementing this you can use your class object with using:

    using (EmployeeWithIDisposable obj = new EmployeeWithIDisposable())

                    {

                        obj.name = "name";

                        obj.address = "s";

                        obj.obj.address = "new address";

                    }

    Once end of region is reached, GC will call dispose method automatically.

    Using foreach loop instead of using tradition loop syntax

    Foreach loop let you to traverse each object with in any collection, with this you can execute required operation on any object, instead of using traditional for or while loop using some integer variable representing index of collection objects, and below is example:

    List<EmployeeWithIDisposable> objEmp = new List<EmployeeWithIDisposable>();

     

    objEmp.Add(new EmployeeWithIDisposable { name = "yogi", address = "1298", obj = new Employee { address = "Pkl", name = "yogi" } });

     

    objEmp.Add(new EmployeeWithIDisposable { name = "Sunil", address = "121", obj = new Employee { address = "bl", name = "Sunil" } });

     

    objEmp.Add(new EmployeeWithIDisposable { name = "Surya Kant", address = "Bangalore", obj = new Employee { address = "bl", name = "surya" } });

     

    objEmp.Add(new EmployeeWithIDisposable { name = "Srini", address = "Chennai", obj = new Employee { address = "chn", name = "Srini" } });

     

    foreach (var d in objEmp)

    {

       Console.WriteLine(d.name);

    }

    Case insensitive comparision

    A lot of time we need to make case insensitive comparison on string objects especially in case of data coming from database as SQL server is by default case insensitive. So for doing case insensitive comparisons people practice converting both objects either in upper case or lower case before comparison, which is not considered as good practice, you can do case insensitive comparison using below syntax too:

    string name = "yogi";

    string copyName = "yOgi";

    bool isEqual = name.Equals(copyName, StringComparison.OrdinalIgnoreCase);

    int isEqualCompare = string.Compare(name, copyName, false);

    int isEqualCompareCulture = string.Compare(name, copyName, new CultureInfo("en-US"), CompareOptions.IgnoreCase);

    Using Convert.ToString instead of using .ToString()

    Using .ToString() for conversion can lead into run time exception as .ToString() return exception if object you are casting is null. So to avoid that you can use Convert.ToString() for converting object to string, this will return empty string in case of null passed.

    Below code will give exception:

    try {

        object objNull = null;

        string s = objNull.ToString();

       }

       catch(Exception ex)

       {

         Console.WriteLine(ex.Message);

       }

    Below code will run without exception:

    try {

          object objNull = null;

          string s = Convert.ToString(objNull);

    }

    catch(Exception ex)

    {

              Console.WriteLine(ex.Message);

    }

     

    Overriding Equals method

    A lot of time we have to compare objects of custom classes, for comparing those you cannot use .Equals directly as it will always give true if object types are same but it will not check for values of properties within object, so to accomplish this you can override .Equals method or you can create overloaded method too below is example for it:

    Override method

        public class DBClass : System.Object

        {

            public string tablename { get; set; }

            public string schemaname { get; set; }

            public override bool Equals(System.Object obj)

            {

                // If parameter is null return false.

                if (obj == null)

                {

                    return false;

                }

     

                // If parameter cannot be cast to Point return false.

                DBClass p = obj as DBClass;

                if ((System.Object)p == null)

                {

                    return false;

                }

     

                // Return true if the fields match:

                return this.schemaname.Equals(p.schemaname, StringComparison.OrdinalIgnoreCase) && this.tablename.Equals(p.tablename, StringComparison.OrdinalIgnoreCase);

            }

        }

     

    Overloaded method

        public class DBCloneClass

        {

            public string tablename { get; set; }

            public string schemaname { get; set; }

            public bool Equals(DBCloneClass obj)

            {

                // Return true if the fields match:

                return this.schemaname.Equals(obj.schemaname, StringComparison.OrdinalIgnoreCase) && this.tablename.Equals(obj.tablename, StringComparison.OrdinalIgnoreCase);

            }

        }

     

    Code to test:

     //Equals Override

      DBClass objDBClass = new DBClass();

      objDBClass.tablename = "TableMe";

      objDBClass.schemaname = "DBO";

      DBClass objDBClassClone = new DBClass();

      objDBClassClone.tablename = "TableMe";

      objDBClassClone.schemaname = "dbo";

      var resultBool = objDBClass.Equals(objDBClassClone);

     

     //Defining Equals Method

      DBCloneClass objDBCloneClass = new DBCloneClass();

      objDBCloneClass.tablename = "TableMe";

      objDBCloneClass.schemaname = "DBO";

      DBCloneClass objDBCloneClassClone = new DBCloneClass();

      objDBCloneClassClone.tablename = "TableMe";

      objDBCloneClassClone.schemaname = "dbo";

      var resultBoolCloneClass = objDBCloneClass.Equals(objDBCloneClassClone);

     

    SQL Helper Class

    This class comes under Microsoft Data Access Application Blocks. This class is open source and can be found in raw code form too. Also this is great class to minimize your ADO.NET code. You don’t have to declare connection object then command object and then parameters, instead you can do all in one single line using this helper class.

    Cs Link:

    https://code.google.com/p/cs-dotnetnuke/source/browse/trunk/CS_Library/Components/ApplicationBlocks/SQLHelper.cs?r=37

    Below is example:

    Selecting from database

    DataSet ds=new DataSet();

    ds = SqlHelper.ExecuteDataset( CodingExample.Properties.Settings.Default.NorthwindConnectionString,CommandType.Text, "SELECT * FROM CATEGORIES");

     

    Inserting into database and getting generated identity.

     

    DataSet dsInsertAndGettingID = new DataSet();

    dsInsertAndGettingID=SqlHelper.ExecuteDataset( CodingExample.Properties.Settings.Default.NorthwindConnectionString,CommandType.Text, "INSERT INTO [Categories]([CategoryName],[Description])    VALUES('NewCatNew','New Category Example'); SELECT SCOPE_IDENTITY()");

                   

    Other methods are also there which you can use according to your requirement

    SqlHelper.ExecuteNonQuery

    SqlHelper.ExecuteReader

    SqlHelper.ExecuteScalar, etc.

    Using LINQ for querying collection

    You can avoid loops using LINQ if you want to query any collection, string is basic collection of characters so first example below is on string, also you can query other query able collections. Below is example:

    //String is collection of characters

    string myDetail = "My--Name--Is--Yogesh.";

    var resultDetail = from i in myDetail where i != '-' select i;

    string resultFullDetail = string.Concat(resultDetail);

     

     

    List<EmployeeWithIDisposable> objEmp = new List<EmployeeWithIDisposable>();

    objEmp.Add(new EmployeeWithIDisposable { name = "yogi", address = "1298", obj = new Employee { address = "Pkl", name = "yogi" } });

    objEmp.Add(new EmployeeWithIDisposable { name = "Sunil", address = "121", obj = new Employee { address = "bl", name = "Sunil" } });

    objEmp.Add(new EmployeeWithIDisposable { name = "Surya Kant", address = "Bangalore", obj = new Employee { address = "bl", name = "surya" } });

    objEmp.Add(new EmployeeWithIDisposable { name = "Srini", address = "Chennai", obj = new Employee { address = "chn", name = "Srini" } });

     

    //Querying Employee List.

    var resultEmploye = from i in objEmp where i.name.StartsWith("Y", StringComparison.OrdinalIgnoreCase) select i;

    var resultEmployeeSorted = from i in objEmp orderby i.name descending select i;

     

    //Querying DataGridView

    var resultDataGridView = from i in dataGridView1.Rows.Cast<DataGridViewRow>() select new { data = i.Cells[0].Value + " " + i.Cells[1].Value };

     

     

    //Generating comma seperated output

    var resultComma=string.Join(",",resultDataGridView.Select(i=>i.data).ToArray());

     

    Some Common Tips

    First, Using properties rather than accessing app config using configuration manager to avoid run time exceptions, for this you need you watch above video in which I showed how run time exception can ruin you production environment

    Second, writing come common libraries for encryption, IO operations which you frequently use in your projects as it will provide you code reusability.

    Cross Resolution designs in windows forms application

    In windows forms application you can create cross resolution applications by simply using some container controls like table layout panel, flow panel, etc with using Dock property properly, I have showed practical example of it in video.

    LINQ operators (extension methods) and database operations

    LINQ is very use full for writing database code and also for generating custom outputs. Below are example of some LINQ database operations:

    Search / Query:

    using (NorthWindDBDataContext objContext = new NorthWindDBDataContext())

    {

        var productList = from i in objContext.Products select i;

        var productListSorted = from i in objContext.Products orderby i.ProductName ascending  select i;

        var firstProduct = (from i in objContext.Products select i).FirstOrDefault();

        var productByCategoryId = from i in objContext.Products where i.CategoryID == 1 select i;

    }

    Inserting Relational data:

    using (NorthWindDBDataContext objContext = new NorthWindDBDataContext())

    {

    Category objCat = new Category();

    objCat.CategoryName = "CategoryNewName";

    objCat.Description="CatDesc";

    objCat.Products.Add(new Product

    {

        ProductName="prodName",

        Discontinued=true,

        QuantityPerUnit="Box",

        ReorderLevel=12,

        SupplierID=1,

        UnitPrice=12,

        UnitsInStock=022,

        UnitsOnOrder=3

    });

    objCat.Products.Add(new Product

    {

        ProductName = "prodName2",

        Discontinued = true,

        QuantityPerUnit = "Box",

        ReorderLevel = 12,

        SupplierID = 1,

        UnitPrice = 12,

        UnitsInStock = 022,

        UnitsOnOrder = 3

    });

    objContext.Categories.InsertOnSubmit(objCat);

    objContext.SubmitChanges();

    }

    Updating and inserting into existing category:

    using (NorthWindDBDataContext objContext = new NorthWindDBDataContext())

    {

        Category objCat = (from i in objContext.Categories where i.CategoryID == 8 select i).FirstOrDefault();

        //objCat.CategoryName = "CategoryNewName";

        // objCat.Description = "CatDesc";

        objCat.Products.Add(new Product

        {

            ProductName = "SeaFoodprodName",

            Discontinued = true,

            QuantityPerUnit = "Box",

            ReorderLevel = 12,

            SupplierID = 1,

            UnitPrice = 12,

            UnitsInStock = 022,

            UnitsOnOrder = 3

        });

        objCat.Products.Add(new Product

        {

            ProductName = "SeaFoodprodName2",

            Discontinued = true,

            QuantityPerUnit = "Box",

            ReorderLevel = 12,

            SupplierID = 1,

            UnitPrice = 12,

            UnitsInStock = 022,

            UnitsOnOrder = 3

        });

        //objContext.Categories.InsertOnSubmit(objCat);

        objContext.SubmitChanges();

    }

    LINQ operators UNION, EXCEPT, INTERSECT and DISTINCT:

    string[] fruits = { "apple", "banana", "mango", "pine", "grapes", "apple", "banana" };

    string[] eatable = { "rice", "flower", "banana", "mango", "grapes" };

              

    var commonDistinct = fruits.Distinct();

    var commonIntersect = fruits.Intersect(eatable);

    var commonUnion = fruits.Union(eatable);

    var commonExcept = fruits.Except(eatable);

    LINQ extension methods:

    string[] words = { "hello", "pello", "dirty", "fellow" };

    var result = words.OrderBy(s => s);

    Int32[] number = { 2, 34, 1, 34, 5, 23, 67, 4, 23, 757, 443, 236, 324 };

    var num = from n in number.OrderByDescending(s => s) select n;

    object[] obj = { 1, "one", 2, "two", 3, "three", 4, "four", 5, "five" };

    var query = obj.OfType<string>();       

    var query1 = obj.OfType<Int32>();

    LINQ aggregate function:

    Int32[] data = { 12, 43, 56, 7, 3, 223, 6, 76, 2, 978, 21, 521 };

    Int32 oddcount = data.Count(n => n % 2 == 1);

    double numsum = data.Sum();

    double max = data.Max();

    double average = data.Average();

    LINQ range generator:

    var numbers = from n in Enumerable.Range(1, 50)

    select new { number = n, oddeven = n % 2 == 1 ? "odd" : "even" };

    LINQ partition and paging:

    string[] partition = { "hello", "made", "for", "this", "male", "female", "make", "take", "skip", "gone", "blob", "happy", "sad", "angry", "kill", "jungle" };

    int pagesize = 5;

    int numberofpages = (int)Math.Ceiling(partition.Count() / (double)pagesize);

    for (int page = 0; page < numberofpages; page++)

    {

        var names = (from p in partition

                        orderby p descending

                        select p).Skip(page * pagesize).Take(pagesize);

        foreach (var name in names)

        {

                       

        }

    }

    LINQ Zip operator:

    string[] name = { "yogi", "harish", "mohit", "sahil" };

    string[] position = { "manager", "team leader", "staff", "engineer" };

    Int32[] experience = { 4, 12, 4, 2 };

    var q = name.Zip(position, (a, b) => a + " is " + b + " of the company ");

    var r = q.Zip(experience, (a, b) => a + " with " + b + " experience");

    var csv = name.Zip(position, (a, b) => a + "," + b).Zip(experience, (a, b) => a + "," + b);

    For download code and slide click here.

    Categories
    Programming [27]
    Tips for programming
    Security [2]
    Security Tips
    Google [1]
    Use google faster then ever you use
    Project [14]
    HTML [2]
    Electronics [0]
    Data Structure [0]
    Database [16]
    SQL SERVER
    SSRS [1]
    Sql Server Reporting Services
    Copyright MyCorp © 2024