Friday, August 10, 2007

Copy Constructor

COPY CONSTRUCTOR
• Copy Constructor is a Constructor that takes an instance of the class as its parameter and sets the data members to match the value of that instance
• C# does not provide a copy constructor

class Person
{
private string name;
private int age;

// Copy constructor.
public Person(Person previousPerson)
{
name = previousPerson.name;
age = previousPerson.age;
}




// Instance constructor.
public Person(string name, int age)
{
this.name = name;
this.age = age;
}

// Get accessor.
public string Details
{
get
{
return name + " is " + age.ToString();
}
}
}

class TestPerson
{
static void Main()
{
// Create a new person object.
Person person1 = new Person("George", 40);

// Create another new object, copying person1, using Copy Constructor
Person person2 = new Person(person1);
System.Console.WriteLine(person2.Details);
}
}

ArrayList > Clone v/s CopyTo

ARRAY LIST METHODS: CLONE v/s CopyTo
• ArrayList.Clone creates a shallow copy of the ArrayList
• ArrayList.CopyTo(Array), copies the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array

Shallow Copy v/s Deep Copy

DEEP COPY v/s SHALLOW COPY
• A shallow copy means the contents contains references to the same as the elements in the original array
• A shallow copy collection copies only the elements of the collection, whether they are reference types or value types
• It DOES NOT copy the objects that references refer to
• The references in the new collection point to the same objects that the references in the original collections point to
• In contrast, a deep copy of a collection copies the elements and everything directly or indirectly referenced by the elements

Thursday, August 9, 2007

Generics > Code Listing

CODE LISTING > GENERIC
public class Test< T >
{
public void Display()
{

}
}

At instantiation of this class, the generic has to be bound to any specific type:
Test< int > objTest = new Test< int >();

After instantiation, the object objText is a bound type.

IMPLEMENTING A GENERIC METHOD
using System;
using System.Collections.Generic;

class Test
{
static void Main( string[] args )
{
Test t = new Test();
int[] integerArray = {1,2,3,4,5,6};
char[] characterArray = { 'J', 'O', 'Y', 'D', 'I','P' };
double[] doubleArray = {0.1,0.2,0.3,0.4,0.5,0.6};
Console.WriteLine( "Displaying the contents of the integer array:--" );
t.Display(integerArray);
Console.WriteLine( "Displaying the contents of the character array:--" );
t.Display(characterArray);
Console.WriteLine( "Displaying the contents of the double array:--" );
t.Display(doubleArray);
}

public void Display< GenericArray >( GenericArray[] array )
{
for (int i = 0; i< array.Length; i++)
Console.WriteLine(array[i]);
}
}

Generics

GENERICS
• New feature in .net 2.0 and CLR
• Found in Namespace: System.Collections.Generic
• Generics are also called parameterized types or parameteric polymorphism
• Generics are a type of data structure that contain code that remains the same; however the data type of the parameters can change with each use
• Generic helps to defer the binding of a generic type to a data type until its point of usage arrives
• Generics introduce to the .net framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code
• Generics in C# support defining algorithms and deferring the data type until the point of use
• In a very general sense, a generic class is just a class that accepts parameters
• Generics allow strongly typed collections, hence letting the compiler do the type checking and hence preventing run time errors caused by type mismatch/casting
• Syntax: Without Generics: class List {} With Generics: class List
• Here T is the Type parameter, T gets defined when object if List is created, hence it can be List or List
• Hence, here T is instantiating at runtime

Nullable Types

NULLABLE TYPES
• Nullable types are instances of the System.Nullable struct
• A nullable type can represent the normal range of values for its underlying value type, plus an additional null value
• For example a Nullable can be assigned any value from -2147483647 to 2147483647, or it can be assigned a null value
• Same with type bool, which can take true, false and null
• Nullable is used while dealing with databases where element may not be assigned a value
• Syntax is: int? x = null or double? d = 4.108;
• To identify a nullable type: int? x = null; Type t = i.GetType(); Console.WriteLine(t.FullName);

ADO.net > DataSet > Typed DataSet

Typed Dataset

Typed datasets derive their schema (table and column structure) from .xsd files and are easier to program against

Typed Databases can be created using Data Source Configuration Wizard

A Typed Dataset is a Dataset that is first derived from the base DataSet class amd then uses information in an XSD file to generate a new class

Information from the schema (tables, columns..) is generated and compiled into this new DataSet class as a set of first class objects and properties

Because a Typed DataSet class inherits from the base DataSet class, the typed class assumes all of the functionality of the DataSet class can be used with methods that take an instance of a DataSet class as a parameter

An untyped dataset has no corresponding built-in schema

The class for a typed dataset has an object model in which its properties take on the actual names of tables and columns

Typed access is not only easier to read, but is fully supported by the IntelliSense in the Visual Studio Code Editor

Typed dataset is not recommended when schema is not available or in situations where structure of data is not static or predictable

Within a dataset, tables and column names are case insensitive, that is Customer and customer table are the same

This matches the default naming conventions of the databases where case does not differentiate the tables/columns

A strongly-typed schema is rigidly defined at compile time, while a loosely typed object, is the one whose schema is not known until runtime

DataReader and DataSet are loosely typed objects and since their schema is defined by the columns returned by the database query used to populate them

A Typed DataSet is a class generated by Visual Studio based on a database schema and whose members are strongly-typed according to this schema

The Typed DataSet itself consists of classes that extend the ADO.net DataSet, DataTable and DataRow classes

In addition to strongly-typed DataTables, Typed DataSets now also include TableAdapters, which are classes with methods for populating the DataSet’s Data Tables and propagating modifications within the Data Tables back to the database