C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




.NET Framework 3.5 Features


Posted Date: 16 Apr 2008    Resource Type: Articles    Category: .NET Framework

Posted By: Gyan Singh       Member Level: Silver
Rating:     Points: 10



Introduction



Multi-Targeting



Visual Studio will now support targeting multiple versions of the .NET Framework, and we will be able to start taking advantage of the new features Visual Studio provides without having to always upgrade their existing projects and deployed applications to use a new version of the .NET Framework library. Using VS 2008, we can create in any of the .Net Framework Versions 2.0, 3.0, or 3.5. The VS 2008 multi-targeting support only works with .NET 2.0, .NET 3.0 and .NET 3.5.

Web Designer and CSS Support


VS 2008 includes a significantly improved HTML web designer. This delivers support for split-view editing, nested master pages, and great CSS integration.
Split View Editing: VS 2008 adds support for a new "split-view" mode when working on pages. This allows us to see both the HTML source and the Design View at the same-time, and easily have any changes we make in one view been updated in the other.
CSS Style Manager: VS 2008 supports a new tool window inside the IDE called "Manage Styles". This shows all of the CSS style sheets, and their corresponding rules, for the page we are currently editing. It can be used both when we are in design-view, as well as when in source view on a page.
CSS Properties Window: One of the other cool new CSS features that is also supported in both design and source view is the new CSS Properties Window. When we select an HTML element or ASP.NET server control, the CSS property window will show us all of the CSS settings currently applied to it. We can also change any of the values using the CSS property grid.
CSS Source View Intellisense: The HTML designer supports the ability to select an element or control in design-view, and graphically select a rule from the CSS list to apply to it.

Nested Master Page Support


One of the feature is the ability to have "nested master pages" - where we can create a root master page for a site that defines a site's overall layout, and then create nested master pages that are based on the root master and further customize the layout. This nested master page feature is extremely flexible, and enables developers and designers to quickly and very cleanly make changes to the layout and organization of a site with minimal code changes and no content duplication. VS 2008 fully supports nested master pages, and makes using them super easy.

ASP.NET AJAX and JavaScript Support


.NET 3.5 has ASP.NET AJAX built-in (and adds new features like UpdatePanel support with WebParts, WCF support for JSON (JavaScript Object Notation), and a number of bug fixes and performance improvements). VS 2008 also has great support for integrating JavaScript and AJAX into the applications.

JavaScript Intellisense


One of the features that we will really like with VS 2008 is its built-in support for JavaScript intellisense. This is enabled in both the free Visual Web Developer 2008 Express edition as well as in Visual Studio, and makes using JavaScript and building AJAX applications significantly easier.

Intellisense for External JavaScript Libraries


VS 2008 supports intellisense not just for in-line script, but also for externally referenced JavaScript files.

Adding Intellisense Hints to JavaScript



function btnClick()
{
/// <summary>This method show confirmation message and returns true/false value. </summary>
/// <returns>string</returns>
var conf = confirm("Do you want to click more?");
return conf;
}

Intellisense within External JavaScript files


One of the interesting characteristics about external JavaScript files is that they can call and use the JavaScript functions and variables declared within other JavaScript files that a page loads

Calling Web Services using ASP.NET AJAX



function callWebService()
{
WebApplication2008.MyService.HelloWorld();
}

JavaScript Debugging in ASP.NET pages


One of the annoying things with VS 2005 is that we have to first run our ASP.NET pages before we can set JavaScript breakpoints in them in the debugger. VS 2008 makes this much better by adding new support that allows us to set client-side JavaScript breakpoints directly within our server-side .aspx and .master source files.

Automatic Properties


Automatic properties allow us to avoid having to manually declare a private field and write the get/set logic -- instead the compiler can automate creating the private field and the default get/set operations for us.

public class Person {
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}

When the C# "Orcas" compiler encounters an empty get/set property implementation like above, it will now automatically generate a private field within class, and implement a public getter and setter property implementation to it. This means that -- unlike public fields -- we can in the future add validation logic within our property setter implementation without having to change any external component that references our class.

Object Initializers



In Previous versions, we write:
Person person = new Person();
person.FirstName = "Gyan";
person.LastName = "Singh";

Now we can write the above code as:

Person person = new Person { FirstName="Gyan", LastName="Singh" };

The compiler will then automatically generate the appropriate property setter code that preserves the same semantic meaning as the previous.

Collection Initializers


With previous version wr write as:

List<Person> people = new List<Person>();
people.Add( new Person { FirstName = "Anoop", LastName = "jain} );
people.Add( new Person { FirstName = "Chandra", LastName = "Prakash" } );
people.Add( new Person { FirstName = "Gyan", LastName = "Singh" } );
Now we can write above code as:
List<Person> people = new List<Person>(){
new Person { FirstName = "Anoop", LastName = "Jain" },
new Person { FirstName = "Chandra", LastName = "Prakash" },
new Person { FirstName = "Gyan", LastName = "Singh" }
};

When the compiler encounters the above syntax, it will automatically generate the collection insert code like the previous sample for us.

Extension Methods


Extension methods allow us to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type. Extension Methods enable a variety of useful scenarios, and help make possible the really powerful LINQ query framework that is being introduced with .NET 3.5. Define Extention Methods as:

public static class ExtensionMethodTest
{
public static bool IsValidEmailAddress(this string s)
{
Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
return regex.IsMatch(s);
}
}
And now we can call it as:
string email = Request.QueryString["email"];
if ( EmailValidator.IsValid(email) )
{
}

LINQ To In-Memory Objects


//Create a list of person

System.Collections.Generic.List<Person> person = new System.Collections.Generic.List<Person> { new Person { FirstName = "A", LastName = "X", Age = 15 }, new Person { FirstName = "B", LastName = "Y", Age = 17 }, new Person { FirstName = "C", LastName = "Z", Age = 24 } };
//Retrieve person whose name starts with "A", display count and FirstName in textbox
IEnumerable<Person> queryResult;
queryResult = person.Where(p => p.FirstName.StartsWith("A"));
txtPerson.Text = "Total Count: " + queryResult.Count().ToString() + Environment.NewLine + "1. " + queryResult.First().FirstName + ", 2. " + queryResult.Last().FirstName;

LINQ To XML

.

Let's assume we have an XML file on disk that contains the data below:

<?xml version="1.0" encoding="utf-8" ?>
<people>
<person age="15">
<firstname>AAA</firstname>
<lastname>XXX</lastname>
</person>
<person age="17">
<firstname>ABB</firstname>
<lastname>YYY</lastname>
</person>
<person age="24">
<firstname>CCC</firstname>
<lastname>ZZZ</lastname>
</person>
</people>
//Using LINQ Extension Methods against an XML File
XDocument people = XDocument.Load(@"C:\Documents and Settings\gyan.singh\Desktop\Venetian\LINQTEST.xml");
//Casting to XElement
System.Collections.Generic.IEnumerable<XElement> xmlResult;
xmlResult = people.Descendants("person").Where(p=>p.Element("firstname").Value.StartsWith("A"));
txtPerson.Text += Environment.NewLine + "XML File" + Environment.NewLine + "2.Total Count: " + xmlResult.Count().ToString() + Environment.NewLine + "Seq 1. " + xmlResult.First().FirstNode + ", Seq 2. " + xmlResult.Last().FirstNode

LINQ To SQL


//LINQ TO SQL Example

LinqToSqlExampleDataContext dbTask = new LinqToSqlExampleDataContext();
var query = from p in dbTask.UserTasks
where p.EmployeeID == 5263
select new { p.EmployeeID,p.Task.Tsk_Message,p.UT_ViewDateTime,p.Task.Tsk_TypeCode,p.Task.Tsk_NavigationURL};
gridView.DataSource = query;
gridView.DataBind();

Lambda Expressions


Lambda Expressions provide a more concise, functional syntax for writing anonymous methods. They are super useful when writing LINQ query expressions - since they provide a very compact and type-safe way to write functions that can be passed as arguments for subsequent evaluation. The p => expressions in above examples is Lambda expression.
In C# a lambda expression is syntactically written as a parameter list, followed by a => token, and then followed by the expression or statement block to execute when the expression is invoked:
params => expression
So when we wrote the lambda expression: p => p.FirstName == "AAA"
We were indicating that the Lambda we were defining took a parameter "p", and that the expression of code to run returns whether the p.FirstName value equals "AAA". The fact that we named the parameter "p" is irrelevant - we could just have easily named it "o", "x", "foo" or any other name we wanted. Lambda parameter types can be inferred at both compile-time and by the Visual Studio's intellisense engine (meaning we get full intellisense and compile-time checking when writing lambdas).

Query Syntax


Query syntax is a convenient declarative shorthand for expressing queries using the standard LINQ query operators. It offers a syntax that increases the readability and clarity of expressing queries in code, and can be easy to read and write correctly. Visual Studio provides complete intellisense and compile-time checking support for query syntax. Under the covers the C# and VB compilers take query syntax expressions and translate them into explicit method invocation code that utilizes the new Extension Method and Lambda Expression language features in "Orcas". We can use query syntax to perform a LINQ query over the collection and fetch only those people whose LastName starts with the letter "Y", sorted by the people's FirstName (in ascending order):
//query syntax
objResult = from p in person
where p.LastName.StartsWith("Y")
orderby p.FirstName
select p;
The query syntax expression above is semantically equivalent to the below code that uses LINQ extension methods and lambda expressions explicitly:
objResult = person.Where(p => p.FirstName.StartsWith("A")).OrderBy(p => p.FirstName);
The benefit with using the query syntax approach is that it ends up being a little easier to read and write. This is especially true as the expression gets richer and more descriptive.
Every syntactic query expression in C# begins with a "from" clause and ends with either a "select" or "group" clause. The "from" clause indicates what data we want to query. The "select" clause indicates what data we want returned, and what shape it should be in.

Anonymous Types


Anonymous types are a convenient language feature of C# and VB that enable developers to concisely define inline CLR types within code, without having to explicitly define a formal class declaration of the type. Anonymous types are particularly useful when querying and transforming/projecting/shaping data with LINQ.

Summary


.Net Framework 3.5 and VS 2008 is more exiting to work with that any previous versions.




Responses


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

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Points  .  LINQ  .  Featuers of dot net 3.5  .  

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: Show all GridView Rows in EditMode
Previous Resource: ADO.NET Connection Pooling at a Glance
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

teleconferencing service

Contact Us    Privacy Policy    Terms Of Use