Search This Blog

Thursday, October 24, 2013

Types in C#

C# > Types





Difference between Value types and Reference types

Value types
Reference types
Variable content
Value
Reference
Stored
Stack
Heap
Assignment
Value
Reference
Initial value
0, false,’\0’
null






Ranges

Type
Range
byte
0 .. 255
sbyte
-128 .. 127
short
-32,768 .. 32,767
ushort
0 .. 65,535
int
-2,147,483,648 .. 2,147,483,647
uint
0 .. 4,294,967,295
long
-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807
ulong
0 .. 18,446,744,073,709,551,615
float
-3.402823e38 .. 3.402823e38
double
-1.79769313486232e308 .. 1.79769313486232e308
decimal
-79228162514264337593543950335 .. 79228162514264337593543950335
char
Unicode character.
string
String of Unicode characters.
bool
True or False.
object
An object.

ICloneable system interface

C# > System > Interfaces > IEquatable

The ICloneable interface enables you to provide a customized implementation that creates a copy of an existing object.

Definition:

public interface ICloneable
    {
        // Summary:
        //     Creates a new object that is a copy of the current instance.
        //
        // Returns:
        //     A new object that is a copy of this instance.
        object Clone();
    }

Example:
        public class Customer : ICloneable
        {
            public string Name { get; set; }
            public Address CustomerAddress { get; set; }
            public object Clone()
            {
                Customer newCustomer = (Customer)this.MemberwiseClone();
                newCustomer.CustomerAddress = (Address)this.CustomerAddress.Clone();
                 return newCustomer;
            }
        }
        public class Address : ICloneable
        {
            public string StreetName { get; set; }
            public int StreetNumber { get; set; }
            public object Clone()
            {
                return this.MemberwiseClone();
            }
        }

        Customer c1 = new Customer();
        c1.Name = "c1";
        Address a1 = new Address();
         a1.StreetName = "Street1";
        a1.StreetNumber = 1;
        c1.CustomerAddress = a1;

        Customer custClone = (Customer)c1.Clone();
        custClone.Name = "c2";

 




Factory Design Pattern C#

C# > Design Patterns > Factory

The factory completely abstracts the creation and initialization of the product from the client.

This indirection enables the client to focus on its role in the application without concerning itself with the details of how the product is created.

In this way the product implementation changes over time and the client remains unchanged.

Model:

Client -> (uses) -> Factory -> (creates) - > Product

 

Example:

Database factory class

                public enum DbOptions
        {
            SQLServer,
            Oracle
        }

        public interface IDatabase
        {
            string Connect();
        }

        public class clsSQlServer : IDatabase
        {
            public string Connect()
            {
                return ("You connected to SQlServer");
            }
        }
        public class clsOracle : IDatabase
        {
            public string Connect()
            {
                return ("You connected to Oracle");
            }
        }
        public class clsUnknown : IDatabase
        {
            public string Connect()
            {
                return ("Unknown database");
            }
        }

        public class DatabaseChoice // Factory class
        {
            static public IDatabase getDBObj(DbOptions db)

            {
                IDatabase objDb = null;
                if (db == DbOptions.SQLServer)
                {
                    objDb = new clsSQlServer();
                }
                else if (db == DbOptions.Oracle)
                {
                    objDb = new clsOracle();
                }

                // In the future if will use MySql only add here
                //else if (db == DbOptions.MySql)
                //{
                //    objDb = new clsMySql();
                //}
                else
                {
                    objDb = new clsUnknown();
                }
                return objDb;
            }
         }
// client

IDatabase objDb = DatabaseChoice.getDBObj(DbOptions.Oracle);
MessageBox.Show(objDb.Connect());

     





Wednesday, October 23, 2013

Events in C#

An event is a message sent by an object to signal the occurrence of an action.
An event in C# is a way for a class to provide notifications to clients of that class when some interesting thing happens to an object.
The most use for events is in graphical user interfaces and the action could be caused by user interaction or it could be triggered by some other program logic.

Example:

// Declare a delegate for an event. 
delegate void EventHandler();

class Product
{
public string Name { get; set; }
       private int mQuantity;
       public int Quantity
       {
            get
            {
                  return mQuantity;
             }
             set
             {
                    mQuantity = value;
                    OnQuantityChange();
              }
       }

      // Declare event. 
       public event EventHandler QuantityChangeEvent;

       // This is called to fire the event.
       public void OnQuantityChange()
       {
              if (QuantityChangeEvent != null)
                     QuantityChangeEvent();
             }
}

 
static void handler()
{
      MessageBox.Show("Quantity changed!"); 
}

Product p = new Product();

//Add handler() to the event list.
p.QuantityChangeEvent += new EventHandler(handler);
p.Name = "Prod A";
p.Quantity = 10;
p.Quantity = 0;

<< Anonymous functions