Search This Blog

Friday, June 15, 2018

Char Vs NChar Vs Varchar Vs NVarchar




Char
NChar
Varchar
NVarchar
Length Type
Fixed
Fixed
Variable
Variable
Unicode Supported
No
Yes
No
Yes
Space occupy per character
1
2
1
2

Encapsulation Vs Abstraction



Encapsulation
Abstraction
Hide data members and member functions within entity (called class) from outside
Show only relevant data and hide the irrelevant data within the scope
Hide information
Hide Implementation
Protection Purpose
Complexity Reduction Purpose
Achieve using access modifiers
Achieve using interface and/or abstract classes
Implement on implementation level
Implement on design level

Encapsulation: Hide data members and member functions within entity/unit (called class) from outside the world, for hiding information in the purpose of protecting data on implementation level and this hide information is achieved through access modifiers.

Abstraction: Display only relevant information and hide the irrelevant data. E.g. only declaration of the method without implementation. So that it can be known that what are the methods within the class but how they will work is not known and by this way it reduces the complexity of the code. This is achieve using interface and/or abstract classes on design level.

Thursday, June 14, 2018

Sealed Method Vs Private Method

Please go through the Sealed method details first before.

Private method is used to restrict the access of the method outside the class, whereas Sealed method is used to restrict the further override of the method.

Private method can not be accessed from child class, but Sealed method can be accessed from child class as well, but Sealed method can avoid further redefining and overriding of the method.

E.G.

namespace SealedMethodDemo
{
    class ParentClass
    {
        private void PrivateMethod()
        {

        }

        protected void ProtectedMethod()
        {

        }
        public void PublicMethod()
        {

        }

        public virtual void SealedMethod()
        {

        }
    }

    class ChildClass : ParentClass
    {
        public void Test()
        {
            PublicMethod();
            ProtectedMethod();
            SealedMethod();// Can access Sealed method but can not access PrivateMethod
        }

        public sealed override void SealedMethod()
        {
            base.SealedMethod();
        }
    }
    class Program: ChildClass
    {
        static void Main(string[] args)
        {
            ChildClass childObject = new ChildClass();
            childObject.SealedMethod();
        }

        //public override void SealedMethod() //cannot override inherited member because it is sealed
        //{

        //}
    }
}


In ChildClass we can access the SealedMethod but we can not access the PrivateMethod in the above example.

Sealed Method


Sealed keyword is basically used to prevent from inheritance. If class is declared as Sealed that means that class cannot be inherited. Similarly, if a method is declared as Sealed that means that method cannot be override in the further inherited class method. Sealed in method is always used with override keyword.

Sealed method is used to define the overriding level of a virtual method. By declaring method as sealed, we can avoid further redefining and overriding of this method. In other word's we use "sealed" on a method if we didn't want any derived classes to further override out method. A sealed, overridden method prevents additional overriding.

The sealed modifier in the method always used with override. Because the intention behind sealing a method is to stop further overriding of it.

In other words, at any level of inheritance, if you want to restrict the next level of derived classes from overriding a virtual method, then create that method by using the keyword sealed along with the keyword override.

Abstract modifier cannot be use with sealed class, because an abstract class must be inherited by a class that provides an implementation of the abstract methods or properties. When applied to a method or property, the sealed modifier must always be used with override.

1.       Because structs are implicitly sealed, they cannot be inherited.

2.       It is not necessary that a sealed class should also contain sealed method. As, sealed class itself means that no class can be derived or extended from it, so, there is no question that a method can be overridden and implemented by derived class. Hence, there is no sense of having sealed method it.

3.       Any method can be sealed in the normal class which allow extension, so no derived class can override its own implementation. In very simple word, if I don’t allow a derived class to override it accidentally and write his own definition, I will make it sealed. “Accidentally”, I said because if you can remove the sealed keyword in base class method and override it in your derived class but you will think of the intention of making it sealed and act accordingly in the program. By the way this is the best practice.

4.       Sealed class and sealed method in C# programming is used to stop a developer to extend a class or override a method accidentally. If he does so, C# compiler will flash an error. If he really wants to extend or override he can remove the sealed keyword in C# program.

If we try to override sealed method then it will throw compile time error that “cannot override inherited member because it is sealed”



E.G.
class ChildClass : ParentClass
    {
        public void Test()
        {
            PublicMethod();
            ProtectedMethod();
            SealedMethod();
        }
        public sealed override void SealedMethod()
        {
            base.SealedMethod();
        }
    }

    class Program: ChildClass
    {
        static void Main(string[] args)
        {
            ChildClass childObject = new ChildClass();
            childObject.SealedMethod();
        }
        //public override void SealedMethod() //cannot override inherited member because it is sealed
        //{
        //}
    }

Sealed Method Vs Private Method

Friday, March 30, 2012

DataTable Comparision

To Compare Two DataTable it is not possible as
(DTable1 == DTable2)

You can try this function
ComareDataTables(DTable1,DTable2);

private bool CompareDataTables(DataTable DT1, DataTable DT2)
        {
            if ((DT1 == null) && (DT2 == null))
                return true;
            else if ((DT1 != null) && (DT2 != null))
            {
                if (DT1.Rows.Count == DT2.Rows.Count)
                {
                    if (DT1.Columns.Count == DT2.Columns.Count)
                    {
                        for (int i = 0; i < DT1.Rows.Count; i++)
                        {
                            for(int j = 0; j<DT1.Columns.Count; j++)
                            {
                                if (DT1.Rows[i][j].ToString() != DT2.Rows[i][j].ToString())
                                    return false;
                            }
                        }
                        return true;
                    }
                    else
                        return false;
                }
                else
                    return false;
            }
            else
                return false;
        }

Monday, January 30, 2012

SQL Connection Class

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess
{
    public class SQLDAL
    {
        string SQLConnectionString;

        public SQLDAL()
        {
            SQLConnectionString = string.Empty;
        }
        public SQLDAL(string SQLConnectionString)
        {
            this.SQLConnectionString = SQLConnectionString;
        }
        public enum ExecutionType
        {
            ReturnNothing = 1,
            ReturnScalarValue = 2
        }
        private string ModifiedSQLQuery(string argSQLQry)
        {
            argSQLQry = argSQLQry.Replace(" Function ", "[Function]");
            argSQLQry = argSQLQry.Replace(" Function,", "[Function],");
            argSQLQry = argSQLQry.Replace(".Function", ".[Function]");
            argSQLQry = argSQLQry.Replace(" Status ", "[Status]");
            argSQLQry = argSQLQry.Replace(" Status,", "[Status],");
            argSQLQry = argSQLQry.Replace(".Status", ".[Status]");
            argSQLQry = argSQLQry.Replace("[", "(");
            argSQLQry = argSQLQry.Replace("]", ")");
            return argSQLQry;
        }
        public PropertyCollection ConnectionString
        {
            get;
            set;
        }
        /// <summary>
        /// Get DataSet
        /// </summary>
        /// <param name="argSQLQry"></param>
        /// <returns>DataSet</returns>
        public DataSet GetDataSet(string argSQLQry)
        {
            try
            {
                using (DataSet dataset = new DataSet())
                {
                    argSQLQry = ModifiedSQLQuery(argSQLQry);
                    SqlDataAdapter adapter = new SqlDataAdapter(argSQLQry, SQLConnectionString);
                    adapter.Fill(dataset);
                    return dataset;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// Get DataTable
        /// </summary>
        /// <param name="argSQLQry"></param>
        /// <returns>DataTable</returns>
        public DataTable GetDataTable(string argSQLQry)
        {
            try
            {
                using (DataTable datatable = new DataTable())
                {
                    argSQLQry = ModifiedSQLQuery(argSQLQry);
                    SqlDataAdapter adapter = new SqlDataAdapter(argSQLQry, SQLConnectionString);
                    adapter.Fill(datatable);
                    return datatable;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// Execute SQL Command
        /// </summary>
        /// <param name="argSQLQry"></param>
        /// <param name="executionCommandType"></param>
        /// <returns>Object</returns>
        public object ExecuteSQLCommand(string argSQLQry, ExecutionType executionCommandType)
        {
            try
            {
                using (SqlConnection ConnectionString = new SqlConnection(SQLConnectionString))
                {
                    ConnectionString.Open();
                    using (SqlCommand command = new SqlCommand(argSQLQry, ConnectionString))
                    {
                        object obj = DBNull.Value;
                        if (executionCommandType == ExecutionType.ReturnNothing)
                            obj = command.ExecuteNonQuery() as object;
                        else
                            obj = command.ExecuteScalar() as object;
                        return obj;
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public object ExecuteSQLCommand(string argSQLQry, List<SqlParameter> sqlParamCollection, ExecutionType executionCommandType)
        {
            try
            {
                using (SqlConnection ConnectionString = new SqlConnection(SQLConnectionString))
                {
                    ConnectionString.Open();
                    using (SqlCommand command = new SqlCommand(argSQLQry, ConnectionString))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        foreach (SqlParameter param in sqlParamCollection)
                        {
                            command.Parameters.Add(param);
                        }

                        object obj = DBNull.Value;
                        if (executionCommandType == ExecutionType.ReturnNothing)
                            obj = command.ExecuteNonQuery() as object;
                        else
                            obj = command.ExecuteScalar() as object;
                        return obj;
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

    }

    public class CommandObj
    {
        string ParameterName { get; set; }
        SqlDbType ParameterType { get; set; }
        string Parameter { get; set; }
    }
}