Search This Blog

Tuesday, June 17, 2014

Difference between a structure and a class


  • Structures are value type and Classes are reference type.
  • Structures can not have constructors or destructors. Classes can have both constructors and destructors.
  • Structures do not support Inheritance, while Classes support Inheritance.
  • Classes are usually used for large amounts of data, whereas structs are usually used for smaller amounts of data.
  • A structure couldn't be null whereas a class can be null.
  • A structure can't be abstract whereas a class can be abstract.

Monday, June 16, 2014

Difference between Dataset.clone and Dataset.copy in C#

Dataset.clone copies just the structure of dataset (including all the datatables, schemas, relations and constraints). It doesn't copy the data.

Dataset.copy copies both the dataset structure and data.

Sunday, June 15, 2014

Difference between Constant and ReadOnly in C#

Constant:
--------

  1. Constant value is declared using "const" keyword.
  2. A const needs to be declared and initialized at declaration only.
  3. A const’s value is evaluated at design time.
  4. A const can not be static.
const string name = "Jeetendra";

ReadOnly:
--------

  1. ReadOnly value is declared using "readonly" keyword.
  2. A readonly can be initialized at declaration or by the code in the constructor.
  3. A readonly’s value is evaluated at runtime.
  4. A readonly can be static.
readonly string name = "Jaiswal";

Sealed class in C#


  1. A sealed class is a class which cannot be inherited by any class.
  2. In C#, we can use the "sealed" keyword to define a class as sealed.
  3. Sealed class is used when we don't want our class to be inherited by any other class.
SealedExample.cs
----------------

 public sealed class SealedExample
    {
        public int Result(int num1, int num2)
        {
            return num1 * num2;
        }
    }


Program.cs
----------
class Program
    {
        static void Main(string[] args)
        {
            SealedExample se = new SealedExample();
            Console.WriteLine("Result: {0}", se.Result(7, 3));
        }
    }

Set maximum limit to the number of objects that can be created for a class

Employee.cs
-----------------
public class Employee
    {
       //Variable to maintain the total number of objects created
        private static int numberOfObjects = 0;

        protected Employee()

        {
            Console.WriteLine("Employee {0} created", numberOfObjects);
        }

        //Check the total number of objects created.

        //If count is <5, then create an object
        //Else return null.
        public static Employee createEmployee()
        {
            if (numberOfObjects++ < 5)
            {
                return new Employee();
            }
            else
            {
                return null;
            }
        }
    }


Program.cs

----------------
class Program
 {
  static void Main(string[] args)
  {                                                                                                            //Output
   Employee emp1 = Employee.createEmployee();//Employee 1 created
   Employee emp2 = Employee.createEmployee();//Employee 2 created
   Employee emp3 = Employee.createEmployee();//Employee 3 created
   Employee emp4 = Employee.createEmployee();//Employee 4 created
   Employee emp5 = Employee.createEmployee();//Employee 5 created
   Employee emp6 = Employee.createEmployee();//null
  }
}

Saturday, June 14, 2014

Object Oriented Programming (OOPS) Concepts in C#

Class:
  1. A grouping of related objects.
  2. A blueprint used for producing objects having a similar set of attributes & behavior, but which can be uniquely identified.
  3. A user defined data type.
public class Employee
{
    protected int empID;
    protected string name;
    protected string address;
      
      public void display()
      {
         Console.WriteLine("Employee Class");
     }
}


Object:

  1. A real world entity that is identified by simulating the human thought process.
  2. A Real world entity that has
  •   State
  •   Identity &
  •   Behavior
Employee emp new Employee();

"emp" is the object of "Employee" class.




OOPS concepts:

1) Abstraction:
  1. Abstraction is
    • What you expose to the end user
    • What an Object does
  2. Abstraction refers to the description of externally visible behavior of an Object.
  3. Abstraction is used to describe what an Object does rather than how it does it.
  4. Abstraction is a process of hiding the implementation details and displaying the essential features.
Example: Consider an ATM machine. 
We are exposed only the screen with various options through which we can do many kind of transactions like check balance, withdraw amount, etc.
We don't need to know how it internally processes this complex transactions by checking the Total amount available, withdrawal amount not more than available amount, Net amount left is not less than minimum balance limit, etc.
So, the user friendly screen with various options is exposed to the end user and this refers to abstraction.


2) Encapsulation:


  1. Encapsulation is
    • What you hide from the end user
    • How an Object does what it does
  2. Encapsulation is a description of the internal data structure & implementation that gives rise to the externally visible behaviour.
  3. Encapsulation hides the details of the implementation of an Object.
Example: Consider an ATM machine. 
We are exposed only the screen with various options through which we can do many kind of transactions like check balance, withdraw amount, etc.
We don't need to know how it internally processes this complex transactions by checking the Total amount available, withdrawal amount not more than available amount, Net amount left is not less than minimum balance limit, etc.
So, the complex transactions done by the ATM internally is hidden from the end user and this refers to encapsulation.



3) Inheritance:

  1. Extend rather than modifying
  2. Do not break what is already working
  3. Reuse rather then re-invent
  4. Inheritance enables you to create a new class that reuses, extends, and modifies the behavior that is defined in another class.
Public class Parent
{
    Public Parent()
    {
        Console.WriteLine ("Parent class");
    }
                                 
    Public void Display()
    {
        Console.WriteLine ("Parent method");
    }
}
                                 
Public class ChildParent
{                                 
    Public Child()
    {
        Console.WriteLine("Child Class");
    }
   
    Public static void Main ()
    {
        Child obj = new Child();
        obj.Display();
    }
}

Output:
  Base Class
  Child Class
  
Parent method


3) Polymorphism:
  1. One form – multiple behaviour.
  2. Enabling the client to select which code block to execute dynamically without breaking the client.
  3. Enables the client to dynamically replace the object to be operated upon without the breaking the client.
  4. Classes provide different implementations of methods having the same name. 
  5. Polymorphism is of two types:
    • Method Overloading (Compile time polymorphism)
    • Method Overriding (Runtime polymorphism)


Friday, June 13, 2014

Difference between Abstract Class and Interface

SrNo Abstract Class Interface
1
Abstract class is a class that contain complete 
and abstract (incomplete) both types of members
Interface is a type which contains only the signatures of methods, delegates or events
2
Abstract class can be inherited by other classes 
but cannot be instantiated.
Interfaces are implemented by a class
3
A class can inherit only one abstract class A class can implement multiple interfaces
4
Abstract class can contain data members, constructors Interface doesn't contain data members, constructors
5
We can not achieve multiple inheritance  We can achieve multiple inheritance
6
Can contain access modifiers like public, private, protected, etc. for functions, snubs, properties. Doesn't contain access modifies. By default, everything is public.
7
Members of abstract class can be static. Members of interface cannot be static.

Thursday, June 12, 2014

Serialization and Deserialization in c#

Serialization is the process of converting an object into stream of bytes for storage and data transmission.
Deserialization is the reverse process of converting stream of bytes to their original form.

Advantages of Serialization:
  1. Facilitate the transportation of an object through a network
  2. Create a clone of an object
Disadvantage of Serialization:
  1. Resource overhead that is involved in serializing and deserializing the data.
  2. Latency issues that are involved for transmitting the data over the network.
  3. Serialization is a slow process.

Tuesday, June 3, 2014

Restore backup in SQL Server using SQL Scripts


1) Place the backup file <SampleBackupFile.bak> to an accessible location <E:\SampleFolder>.



2) Retrieve the mdf and ldf file names of the database from the backup file using the below query.


RESTORE FILELISTONLY

FROM DISK = 'E:\SampleFolder\SampleBackupFile.bak'


3) Now restore the database <SampleDB> using the below query.


RESTORE DATABASE <SampleDB>

FROM DISK = 'E:\SampleFolder\SampleBackupFile.bak'
WITH MOVE 'mdf_filename' 
        TO 'E:\SampleFolder\SampleMDFFile.mdf',
     MOVE 'ldf_filename' 
        TO 'E:\SampleFolder\SampleLDFFile.ldf'

Sunday, June 1, 2014

TRIM function in SQL Server

The TRIM function in SQL Server is used to remove white spaces.

There are 3 types of TRIM:
1)TRIM(<InputString>) - Removes white spaces from both left and right side of the input string.
2)LTRIM(<InputString>) - Remove white spaces from left side of the input string.
3)RTRIM(<InputString>) - Removes white spaces from right side of the input string.

Type Query Output
TRIM SELECT TRIM(' Hello ') Hello
LTRIM SELECT TRIM(' Hello') Hello
RTRIM SELECT TRIM('Hello ') Hello

Search any SP/Table/View/Function in SQL Server

SELECT DISTINCT so.name [Name], so.xtype [Type]
FROM sysobjects so INNER JOIN syscomments sc 
     on so.id = sc.id
WHERE sc.text like '%<SearchText>%'

Reverse a string without using string function in C#

------------------------------------------
Input String -> "Hello"
Expected Output -> "olleH"
------------------------------------------

string inputString = "Hello";
string outputString = "";
char[] array inputString.ToCharArray();
Array.Reverse(array);
outputString  = new string(array);
------------------------------------------