C# Essential Language Features Part 2
This is continuous
of Series “C# Essential
Language Features”. Interested in Part 1,click here .We are going to explore more advanced C# Language features in
this article. I will be covering some features which are good to know as
general and while implementing the MVC application.
Automatic Type Inference
Var Keyword
was introduced in C# 3.0, which allows you to define the variable without
specifying the variable type as shows in Listing 1. This is called type inference or implicit typing.
Listing
1
static void Main(string[] args)
{
var myProduct = new Product { Name = "Dell
XPS", Category = "Laptop",
Price = 275M };
Console.WriteLine("Name
of Product is " + myProduct.Name);
}
In the
above Listing we are using the Product class as we used in our Part 1. In past
we always create object of Product class and define explicitly that it is
object of Product like
Product myproduct = new Product
{Name = "Dell XPS", Category = "Laptop", Price = 275M };
myProduct
in Listing 1 has the type and it is strong type as it will only allow you to
access only the member of the Product Class and not anything else.
LINQ (Language Integrated Queries)
LINQ is
also introduced in .Net 3.5. LINQ makes a query
a first-class language construct in C# or VB. LINQ is the similar to SQL
like structure/syntax for querying strongly typed data. You can query Objects,
XML, SQL, Datasets and any collections of data which supports IEnumerable or the
generic IEnumerable
interface. Let see how we can query our Shopping Cart and Product data using
LINQ in listing 2
Listing
2
static
void Main(string[]
args)
{
IEnumerable<Product> card = new
ShoppingCart
{
Products = new
List<Product>
{
new Product {Name
= "Dell XPS", Category = "Laptop", Price = 275M},
new Product {Name
= "HP", Category = "Desktop", Price = 48.95M},
new Product {Name
= "ThinkPad", Category = "Laptop", Price = 19.50M},
new Product {Name
= "Acer", Category = "Desktop", Price = 34.95M}
}
};
Console.WriteLine("Using LINQ");
var prod1 = from c in card where
c.Category == "Desktop" select c;
foreach (Product prod in
prod1)
{
Console.WriteLine("Name: {0}, Price {1:c}", prod.Name, prod.Price);
}
Console.WriteLine("Using LINQ Order By");
var prod2 = from c in card orderby
c.Price descending select
c;
foreach (Product prod in
prod2)
{
Console.WriteLine("Name: {0}, Price {1:c}", prod.Name, prod.Price);
}
Console.Read();
}
I have
more explanation of LINQ in my previous article
Anonymous Types
Anonymous
Type is also introduced in C# 3.0. Anonymous Types are created using Inference
and Object Initializers. In other words we can create an object without
declaring the type and class. Since the name of the data type is not declared
or specified the type is referred as Anonymous
Type.
Below
listing shows how to create anonymous type and use that
Listing
3
var myType = new { Company =
"Verint", FirstName = "Kapil" };
Console.WriteLine("Anonymous
Type");
Console.WriteLine(myType.Company);
In Listing 3
myType is an anonymously type object. Looking into the fact is
that it is not a dynamic in nature because the compiler will create a Type definition
automatically. Compiler automatically generates the class with the members
declared so it is strongly typed.
This is the
End of Series “C# Essential Language Features”
No comments:
Post a Comment