Thursday, January 29, 2009
New Features in C# 3.0
Implicitly Typed Local Variables
Local variables can be declared as type ‘var’ which means compiler to determine the actual type based on the data by which its is initialized.
var i = 10; // i is created of type int
var name = “MyName” ; // name is created of type string
can only be used when declared and initialized in same statement.
Cannot be initialized to null.
Cannot be used as class members.
Mostly used to store anonymous types as in LINQ based programming.
Object & Collection Initializers
Allow assigning values to any accessible members or properties of a type at the time of initiation without invoking the constructor with parameters.
The default constructor gets executed before assigning the values.
E.g. Coordinate c1 = new Coordinate {x=1 , y=2};
Used in LINQ query expressions along with anonymous types.
Collection Initializers use Object Initializers to specify multiple elements of collection without calling Add method multiple times.
Extension Methods
Allows adding new methods to existing types without modifying the existing type.
Are special kind of static methods but are called as if they are instance methods.
The first parameter passed to Extension methods specifies to which type they operate on preceded by ‘this’ keyword.
They cannot access the private variables of type which they are extending.
Extension Methods need to defined in a non-nested and non-generic static class.
Instance methods take priority over extension methods in case they have same signature.
Anonymous Types
Are of class types which can have only public read-only properties as their members. No other class members like methods are allowed.
They are of reference types and are derived from ‘Object’ class.
Internally compiler gives them the name but its not accessible by application code.
They have a method scope.
Can be initiated directly e.g. new { property1=1, property2=”Hello World”};
Lambda Expressions
Very similar to anonymous methods introduced in C# 2.0.
Its an inline expression or statement block which can be used to pass arguments to method call or assign value to delegate.
All lambda expression use lambda operator => where the left side denotes result and right contains statement block or expression.
Auto-Implemented Properties
Helps in simplifying property declaration in cases where there is no custom logic required in accessors methods.
E.g. public int Price {get; set;};
Internally compiler creates an anonymous field for assigning values.
In my next post on this i will demonstrate these using a code sample.
Tuesday, January 20, 2009
New in .NET Framework 3.5
1.) CLR Enhancements: Although the CLR uses the same model as 2.0 improvements/changes to the assemblies.
2.) Compiler Enhancements: New VB.NET 9.0 compiler and support for changes to C# 3.0 like expression trees, lambda methods, extension methods, static reference for anonymous types etc.
3.) LINQ: Probably the most revolutionary change in the 3.5 framework. LINQ to XML, LINQ to SQL, LINQ to Objects and LINQ to Datasets. Along with functional programming, LINQ is an outlook change to programming in C#.
4.) Performance Improvements: Quite a few performance improvements have been made in 3.5. ADO.NET gets paging support as well as synchronization from caches at local and server datastores. Also performance improvements for multicore CPUs
5.) Networking changes: Peer-to-peer networking stack, including a managed PNRP resolver.
6.) Windows Information APIs: New wrappers for WMI and Active Directory Services. WMI 2.0 gets a managed provider.
7.) ASP.NET: New implementation of Client Application Services as well as 3 new ASP.NET controls. Also AJAX programming for ASP.NET is easier and better performing.
8.) Windows Communication Foundation: WCF now works with POX and JSON data.
9.) Windows Presentation Foundation: Newer plugin model for creating AddIns. Although SilverLight is separate, I think its still presentation right?? SilverLight CLR is also part of the .Net Framework. I really don't know if XAML gets any language change in the 3.5 framework. I love the XAML designer from VS2008 though!!
10.) Misc: The C/C++ get a standard template libarary (STL) so that these languages can use share .NET libraries
Thursday, January 15, 2009
LINQ in query in C# 3.5
LINQ (Language Integrated Query) is the composition of many standard query operators that allow us to work with data of any datasource in a very intuitive way. LINQ provide compile time checking of query and the ability to debug through query.
To show a very basic example of what can be done with the help of LINQ I am using adding,delete,edit the data and view data.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Configuration;
using LINQ;
using System.Web.UI.WebControls;
namespace Website
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DisplayGridView();
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
linqDataContext db = new linqDataContext();
User user = new User();
user.UserId = Convert.ToInt16(txtUserId.Text);
user.UserName = txtUserName.Text;
db.Users.InsertOnSubmit(user);
db.SubmitChanges();
DisplayGridView();
}
public void DisplayGridView()
{
linqDataContext db = new linqDataContext();
var p = from u in db.Users
select u;
GridView1.DataSource = p;
GridView1.DataBind();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
linqDataContext db = new linqDataContext();
User objUser = db.Users.Single(p => p.UserId == Convert.ToInt16(txtUserId.Text));
objUser.UserName = txtUserName.Text;
db.SubmitChanges();
DisplayGridView();
}
protected void btnDelete_Click(object sender, EventArgs e)
{
linqDataContext db = new linqDataContext();
var p = (from u in db.Users
where u.UserId == Convert.ToInt16(txtUserId.Text)
select u).Single();
db.Users.DeleteOnSubmit(p);
db.SubmitChanges();
DisplayGridView();
}
}
}
Thursday, January 8, 2009
Differences between user controls and custom controls
User control
Deployment
Designed for single-application scenariosDeployed in the source form (.ascx) along with the source code of the applicationIf the same control needs to be used in more than one application, it introduces redundancy and maintenance problems
Creation
Creation is similar to the way Web Forms pages are created; well-suited for rapid application development (RAD)
Content
A much better choice when you need static content within a fixed layout, for example, when you make headers and footers
Design
Writing doesn't require much application designing because they are authored at design time and mostly contain static data
Custom control
Deployment
Designed so that it can be used by more than one applicationDeployed either in the application's Bin directory or in the global assembly cacheDistributed easily and without problems associated with redundancy and maintenance
Creation
Writing involves lots of code because there is no designer support
Content
More suited for when an application requires dynamic content to be displayed; can be reused across an application, for example, for a data bound table control with dynamic rows
Design
Writing from scratch requires a good understanding of the control's life cycle and the order in which events execute, which is normally taken care of in user controls
Monday, January 5, 2009
Serialization in .NET
Serialization is a process of taking an object and converting into a form so that it can be transported across the network or can be persisted in the storage location. This storage location can be physical file, database or ASP.NET Cache. The form contains the state of the object so that by this format, we can construct the same object a later point in time, which is called Deserialization.
There are three formats of serialization
Binary Serialization : Light and compact used in Remoting
SOAP Serialization : interoperable use SOAP and used in web Services
XML Serialization : Custom Serialization
XML SerializationFor XML serialization, you need to use the attributes and specify them for each and every public member that you need. But since it is limited that it can serialize only public members, Serization done by it is called custom serialization. It is also known as Shallow Serialization
SOAP and Binary SerializationXML serializes only public members of the class. You use SOAP or Binary serialization when you need to transport data across the network. SOAP sends it using HTTP Protocol which makes it most interoperable while Binary serialization is known for its light and compact nature. Web Services uses the SOAP Serialization and Remoting uses the Binary Serialization. Infact Serialization is always neccessary when you need the object to transfer across a network. Advantage of using the SOAP or Binary serialization is that you can serialize the entire object and all those object that are being refrenced by it. This is why it is also called Deep Serialization. If you want any class to serialize through any of these methods then you should use [Serializable] attribute on that class and then you can use the SoapFormater class or BinaryFormatter class to do the serialization. These classes have Serialize and DeSerialize method. If you will not use SerializableAttribute for the class, then it will raise the exception.
Though this is the easiest way but at time you need the way so that you can decide what fields to serialize and how the serialization actually occurs. You can implement the ISerializable interface in the class. You need two things for that
Constructor that is overridden and can handle the Deserialization process
GetObject method that tracks about which data is serialized.
Sunday, January 4, 2009
Improving ASP.NET Performance
http://msdn2.microsoft.com/en-us/library/ms998549.aspx