C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Partial Classes , Anonymous methods and Nullable Types


Posted Date: 15 May 2006    Resource Type: Articles    Category: .NET Framework
Author: phanindra yerraMember Level: Gold    
Rating: 1 out of 5Points: 7



Introduction




Microsoft Introduced .Net Framework 2.0, with enhancement of so many new features

1) Language Enhancements:

2) We can run asp.net application without IIS also. By default the applications take support of file system based application server support.


3) Ftp Support: Applications can now access File Transfer Protocol (FTP) resources using the WebRequest, WebResponse, WebClient Classes.

4) HttpListener Class allows u to build simple web server to handle http request and responses.


5) .net framework 2.0 introduce System.Collections.Generic.

6) I/O Enhancements.

7) Typed Dataset and Table Adapter classes.

8) Introduced new controls in toolbox.

And many more …..

Please visit this

http://windowssdk.msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_fxintro/html/2806b1ca-5e02-49c2-b148-f2db96de115d.asp



1) Language Enhancements


.Net Framework 2.0 comes with c# 2.0. (I am concentrating only on C# language).

Why C# 2.0 and what are the new features in it compared to previous version of c#? Let us have a look


Some the New Features in C# 2.0

• Anonymous Method.

• Co Variant and Contra Variant delegates.

• Generics.

• Iterators.

• Yield Statements.

• Partial Classes.

• Nullable Types.

• Accessors Accessability.

• Alias Qualifiers

• Friend Assemblies


For More Information:
Visit this link:
http://windowssdk.msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_csref/html/1f3be563-b68c-4040-b692-d4670a31484f.asp

Here iam explaining AnonymousMethods.Partial classes and Nullable types

Anonymous Method



In the original C# language, to construct a delegate object you have to provide it with the name of a method. The code below shows an example: ‘Print’ is first defined as a particular delegate type, then its instance ‘delegate Variable’ is created by passing it the name of the method ‘real Method’.
Example:
 
delegate void Print (string s);
Print delegateVariable = new Print(realMethod);
public void realMethod (string myString)
{
MessageBox.Show(myString);
}


That means we have to specify separate method definition named realmethod. But incase of anonymous method it is no need.

In anonymous methods, method name is optional and they are able to use where ever delegates are using. (They are some what similar to inline function in c++).

Now, however, C# has been updated to include ‘anonymous’ methods (which should be pretty easy for anyone who has used anonymous functions in languages like Javascript). These allow you to construct a delegate by specifying, rather than just naming a method. The following gives an example of how the above code could be written using an anonymous method:

public delegate void Print (string s);
Print delegateVariable = delegate (string myString)
{MessageBox.Show(myString) ;};

In the above case, the anonymous method is given the same signature as the delegate it is passed to. But this is not always necessary. For the anonymous method to be acceptable, the following two conditions must be met:
1. Either the anonymous method has a parameter list that exactly matches the delegate’s parameters; or the anonymous method has no parameter list and the delegate has no ‘out’ parameters.
2. Either the values returned by the anonymous method are all of the right type for the delegate; or the anonymous method doesn’t return anything and the delegate’s return type is ‘void’.
An implication of the first condition is that an anonymous method with no parameters at all can fit a delegate with parameters. The following code is thus possible (notice that we’ve had to remove the use of ‘myString’ in the Show method, because we’re no longer passing it in):

public delegate void Print (string s);
Print delegateVariable = delegate
{MessageBox.Show(myString) ;};

But incase of accessing parameters in delegate method definition then u should use parameters.

Anonymous methods can also make use of the local variables and parameters in whose scope the anonymous method lies.
Example:

static my Delegate myFunc()
{
int x=0;
myDelegate result = delegate {return ++x;}
return result;
}

For more on Anonymous methods
Visit this
http://msdn.microsoft.com/vcsharp/2005/overview/language/anonymousmethods/


Partial Classes:



It is possible to split the definition of a class (or a struct, or an interface) over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled. There are several situations when splitting a class definition is desirable:
• When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.
• When working with automatically generated source, code can be added to the class without having to re-create the source file. Visual Studio uses this approach when creating Windows Forms, Web Service wrapper code and so on. The programmer can create code that uses these classes, without having to edit the file created by Visual Studio.
• To split a class definition, use the partial keyword modifier, as shown below:


// File: MyClassP1.cs
public partial class MyClass
{
public void method1()
{
}
}
// File: MyClassP2.cs
public partial class MyClass
{
public void method2()
{
}
}

Using the partial keyword indicates that other parts of the class, struct, or interface can be defined within the namespace. All the parts must use the partial keyword. All of the parts must be available at compile time to form the final type. All the parts must have the same accessibility (public, private, etc).
If any of the parts are declared abstract, then the entire type is considered abstract. If any of the parts are declared sealed, then the entire type is considered sealed. If any of the parts declare a base type, then the entire type inherits that class.
All of the parts that specify a base class must agree - but parts that omit a base class still inherit the base type. Parts can specify different base interfaces, and the final type implements all of the interfaces listed by all of the partial declarations. Any class, struct, or interface members declared in a partial definition are available to all of the other parts - the final type is the combination of all the parts at compile time.

• Nested types can be partial, even if the type they are nested within is not partial itself. For example:

class Container
{
partial class Nested
{
int i;
}
partial class Nested
{
int j;
}
}
• Nested partial types are allowed in partial type-definitions, for example:


partial class MyClass
{
partial class NestedClass {}
}
partial class MyClass
{
partial class NestedClass {}
}

Some on partial classes Restrictions

There are several rules to follow when working with partial class definitions:
• All partial-type definitions meant to be parts of the same type must be modified with partial. For example, the following class declarations generate an error:
partial public class A { }
public class A { } // Error, must be marked partial



• All partial-type definitions meant to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file). Partial definitions cannot span multiple modules.
• The class name and generic-type parameters must match on all partial-type definitions. Generic types can be partial. Each partial declaration must use the same parameter names in the same order.
• The following keywords on a partial-type definition are optional, but if present on one partial-type definition, cannot conflict with the keywords specified on another partial definition for the same type:
• public
• private
• protected
• internal
• abstract
• sealed
• base class
• new modifier (nested parts)
• generic constraints

Nullable Types:


A variable of nullable type can represent all the values of its underlying type, plus an additional null value. This is particularly useful when dealing with databases and other data types containing elements that may not be assigned a value, or which may not exist. For example, a Boolean field in a database can store the values true or false, or it may contain neither true nor false.




Nullable Types have the following properties:
• Nullable types are used to create variables that can contain an undefined state.
• The syntax T? is shorthand for System.Nullable, where T is a data type. The two forms are interchangeable.
• The HasValue property returns true if the variable contains a value, or false if it is null.
• The Value property returns a value if one is assigned, otherwise a System.InvalidOperationException is thrown.
• The default value for a nullable type variable sets HasValue to false. The Value is undefined.

Summary



In this i article i explained 3 new features in c#2.0



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: FrameWork Tools
Previous Resource: Coalescing operators ,object initializers and var type in new C#
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use