Friday 6 June, 2008

.Net 3.5 -- C# 3.0 -- Anonymuous Types




The anonymous type has a type name but it is the compiler who generates the name, not the developer itself and you won’t see it in your code.
Anonymous types provide a way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type. The type name is generated by the compiler and is not available at the source code level. The type of the properties is inferred by the compiler.
The following code snippet illustrates that, an anonymous type being initialized with two properties named Code and Name.

C#
var evals = new {code="100" ,Name = "Mark"}

VB.NET
Dim evals = New With {code="100" ,Name = "Mark"}

Anonymous types are typically used in the select clause of a query expression to return a subset of the properties from each object in the source sequence. For more information about queries, see LINQ Query Expressions (C# Programming Guide).
Anonymous types are created by using the new operator with an object initializer. For more information about object initializers, see Object and Collection Initializers (C# Programming Guide).
Anonymous types are class types that consist of one or more public read-only properties. No other kinds of class members such as methods or events are allowed.
If you do not specify member names in the anonymous type, the compiler gives the anonymous type members the same name as the property being used to initialize them. You must provide a name to a property that is being initialized with an expression.
C# var productQuery =
from prod in products

select new { prod.Color, prod.Price };

foreach (var v in productQuery)

{

Console.WriteLine("Color={0}, Price={1}", v.Color, v.Price);

}
శశిధర్

Thursday 29 May, 2008

.Net 3.5 -- What's new in C# 3.0








The following features are added in C# 3.0. I have given brief descriptions about "Implicitly typed local variables" in my previous topic. I am going to give the description about rest of the topics/features in coming dates.
  • Implicitly typed local variables
  • Anonymous types
  • Extension methods
  • Object and collection initializers
  • Auto- Implemented Properties
  • Lambda expressions
  • Query expressions
  • Expression Trees
  • Partial Methods

శశిధర్

Wednesday 28 May, 2008

.Net 3.5 -- C# 3.0 -- Local Type Inference



Local variable type inference is a feature in C# 3.0 where you can use the var keyword instead of explicitly specifying the type of a variable. The C# 3.0 compiler makes the type of the variable match the type of the right side of the assignment. There are places when using C# 3.0 where we must do type inference when declaring variables; there is no other way, so we must cover this topic here.

LINQ query expressions (and many standard query operators) return a type of IEnumerable for some type T. When using LINQ, it is often convenient to make the type parameter be an anonymous type. An anonymous type's name is automatically generated by the compiler, but hidden from the program. If you write a query expression that creates an anonymous type, then the results of the query expression are an IEnumerable of the anonymous type. To declare a variable to hold the results of such a query expression it is necessary to use type inference.



One of the goals of C# 3.0 is to make the language cleaner and smoother. C# 2.0's major innovation was undoubtly generics, but using these introduced quite a bit of "noise" when declaring and instantiating generic types.

It is important to understand that the var keyword does not mean “Variant” and does not indicate that the variable is loosely typed, or late-bound. It just means that the compiler determines and assigns the most appropriate type.
The var keyword may be used in the following contexts:
On local variables (variables declared at method scope) as shown in the previous example.
In a for initialization statement.
for(var x = 1; x < file =" new">
foreach(var item in list){...}
using (var file = new StreamReader("C:\\myfile.txt")) {...}

శశిధర్

Monday 26 May, 2008

.Net 3.5 -- Extension Methods (C#)



Extension Methods:
Extension methods enable you to "Add" mothods to exisiting types without creating a new derived type, recompliling, or otherwise modifying the origiinal type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C3 and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually difined in a type.
శశిధర్

Tuesday 20 May, 2008

LINQ and other .NET Framework 3.5 Improvements

With the addition of Language Integrated Query (LINQ) in .NET Framework 3.5, the process of building SQL queries using error-prone string manipulation is a thing of the past. LINQ makes your relational data queries a first-class language construct in C# and Visual Basic, complete with compiler and Intellisense support. For Web applications, the ASP.NET LinqDataSource control allows you to easily use LINQ to filter, order and group data that can then be bound to any of the data visualization controls like the ListView and GridView controls. In addition, all the other improvements to .NET Framework 3.5, including the new HashSet collection, DateTime offset support, diagnostics, garbage collection, better thread lock support, and more, are all available to you in your ASP.NET applications.
శశి