Subscribe to Subscribers

Resources » .NET programming » .NET Framework

C# Essential Language Features Part 2


Posted Date:     Category: .NET Framework    
Author: Member Level: Gold    Points: 25


This is continuous of Series “C# Essential Language Features”. 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.



This is continuous of Series "C# Essential Language Features". You can look into Part 1 for getting started. 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 card = new ShoppingCart
{
Products = new List {
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 Blog
Here

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"

Thanks for reading this articles.





Did you like this resource? Share it with your friends and show your love!


Responses to "C# Essential Language Features Part 2"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Exception Logging to Database using Enterprise Library 5.0
    Previous Resource: Merge PDF Tool using C# and iTextsharp
    Return to Resources
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2013 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.