Friday, November 20, 2009

.Net Framework 2.0 Features (New)

1.C#.Net 2.0 and ASP.Net 2.0
//-------------------------------

a.New Features in C# 2.0
/////----------------------------------------
1.New Features in C# 2.0
. Generic Types

Generics were added to version 2.0 of the C# language and the common language runtime (CLR). 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. For example, by using a generic type parameter T you can write a single class that other client code can use without incurring the cost or risk of runtime casts or boxing operations, as shown here:

C#
// Declare the generic class.
public class GenericList
{
void Add(T input) { }
}
class TestGenericList
{
private class ExampleClass { }
static void Main()
{
// Declare a list of type int.
GenericList list1 = new GenericList();

// Declare a list of type string.
GenericList list2 = new GenericList();

// Declare a list of type ExampleClass.
GenericList list3 = new GenericList();
}
}

Generics Overview

--------------------------------------------------------------------------------

Use generic types to maximize code reuse, type safety, and performance.

The most common use of generics is to create collection classes.

The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace.These should be used whenever possible instead of classes such as ArrayList in the System.Collections namespace.

You can create your own generic interfaces, classes, methods, events and delegates.

Generic classes may be constrained to enable access to methods on particular data types.

Information on the types that are used in a generic data type may be obtained at run-time by using reflection.

. Iterators

In this example, the DaysOfTheWeek class is a simple collection class that stores the days of the week as strings. After each iteration of a foreach loop, the next string in the collection is returned.

C#
public class DaysOfTheWeek : System.Collections.IEnumerable
{
string[] days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };

public System.Collections.IEnumerator GetEnumerator()
{
for (int i = 0; i < week =" new">, pronounced "Nullable of Int32," can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value. A Nullable can be assigned the values true or false, or null. The ability to assign null to numeric and Boolean types is particularly useful when dealing with databases and other data types containing elements that may not be assigned a value. For example, a Boolean field in a database can store the values true or false, or it may be undefined.

C#
class NullableExample
{
static void Main()
{
int? num = null;
if (num.HasValue == true)
{
System.Console.WriteLine("num = " + num.Value);
}
else
{
System.Console.WriteLine("num = Null");
}

//y is set to zero
int y = num.GetValueOrDefault();

// num.Value throws an InvalidOperationException if num.HasValue is false
try
{
y = num.Value;
}
catch (System.InvalidOperationException e)
{
System.Console.WriteLine(e.Message);
}
}
}

. Anonymous Methods

Creating anonymous methods is essentially a way to pass a code block as a delegate parameter. For example:

// Create a handler for a click event
button1.Click += delegate(System.Object o, System.EventArgs e)
{ System.Windows.Forms.MessageBox.Show("Click!"); };

// Create a delegate instance
delegate void Del(int x);

// Instantiate the delegate using an anonymous method
Del d = delegate(int k) { /* ... */ };

By using anonymous methods, you reduce the coding overhead in instantiating delegates by eliminating the need to create a separate method.

. Covariant and Contravariant Delegates

Covariance and contravariance provide a degree of flexibility when matching method signatures with delegate types. Covariance permits a method to have a more derived return type than what is defined in the delegate. Contravariance permits a method with parameter types that are less derived than in the delegate type.


Example 1 (Covariance)

--------------------------------------------------------------------------------

This example demonstrates how delegates can be used with methods that have return types that are derived from the return type in the delegate signature. The data type returned by SecondHandler is of type Dogs, which derives from the Mammals type that is defined in the delegate.


Class Mammals
{
}

class Dogs : Mammals
{
}

class Program
{
// Define the delegate.
public delegate Mammals HandlerMethod();

public static Mammals FirstHandler()
{
return null;
}

public static Dogs SecondHandler()
{
return null;
}

static void Main()
{
HandlerMethod handler1 = FirstHandler;

// Covariance allows this delegate.
HandlerMethod handler2 = SecondHandler;
}
}

Example 2 (Contravariance)

--------------------------------------------------------------------------------

This example demonstrates how delegates can be used with methods that have parameters of a type that are base types of the delegate signature parameter type. With contravariance, you can now use one event handler in places where, previously, you would have had to use separate handlers. For example, you can now create an event handler that accepts an EventArgs input parameter and use it with the Button.MouseClick event that sends a MouseEventArgs type as a parameter, and also with TextBox.KeyDown event that sends a KeyEventArgs parameter.


System.DateTime lastActivity;
public Form1()
{
InitializeComponent();

lastActivity = new System.DateTime();
this.textBox1.KeyDown += this.MultiHandler; //works with KeyEventArgs
this.button1.MouseClick += this.MultiHandler; //works with MouseEventArgs

}

// Event hander for any event with an EventArgs or
// derived class in the second parameter
private void MultiHandler(object sender, System.EventArgs e)
{
lastActivity = System.DateTime.Now;
}


. Simplified Delegate Instantiation



. Accessor Accessibility

private string name = "Hello";

public string Name
{
get
{
return name;
}
protected set
{
name = value;
}
}


. Namespace Alias Qualifier

using colAlias = System.Collections;
namespace System
{
class TestClass
{
static void Main()
{
// Searching the alias:
colAlias::Hashtable test = new colAlias::Hashtable();

// Add items to the table.
test.Add("A", "1");
test.Add("B", "2");
test.Add("C", "3");

foreach (string name in test.Keys)
{
// Seaching the gloabal namespace:
global::System.Console.WriteLine(name + " " + test[name]);
}
}
}
}

Example 2

class TestApp
{
// Define a new class called 'System' to cause problems.
public class System { }

// Define a constant called 'Console' to cause more problems.
const int Console = 7;
const int number = 66;

static void Main()
{
// Error Accesses TestApp.Console
//Console.WriteLine(number);
global::System.Console.WriteLine("Working");
}
}

. Static Classes
. extern
. Fixed Size Buffers
. Friend Assemblies
. Compiler Pragma



2.Windows Forms in .NET 2.0
. ToolStrip Control
. SplitContainer Control
. Web Browser Control
. DataGridView Control
. Other New .NET 2.0 Controls
. New Data Binding
. Application Settings
. BackgroundWorker Component
. ClickOnce Deployment



3.New Features in ADO.NET and XML
. Asynchronous Database Operations
. Server Enumeration
. Multiple Active Result Sets
. Bulk Copy in ADO.NET
. Edit Capability in XPathNavigator
. Efficient XSLT Processor


b.New Features in ASP.NET 2.0
/////----------------------------------------
1.Fundamentals of ASP.NET 2.0
. ASP.NET 2.0 Applications
. Visual Web Developer
. Using Components
. New Controls
. Menus
. Master Pages



2.Data Access in ASP.NET 2.0
. Data Source Controls
. Connection String Storage
. GridView
. DetailsView
. FormView
. XML Support
. Object Data Sources



3.Personalization and Security in ASP.NET 2.0
. Themes
. Skins
. Membership and Roles
. Login Controls
. User Profiles
. Web Parts

No comments: