.Net & SharePoint '07

Technical blog for .Net and all SharePoint 2007 related Information

About the author

Me(Prince) and my wife are B.E in I.T & C.S.E respectively.  I a certified MCPD: Web from 2007 Dec. I am Intrestes in Web Application, MOSS, EPM, etc.
Now working with Deira International School, as IT Application & Help Manager. I have started my career as "Software Developer" @  REACH Sewn Technologies and Consulting Pvt. Ltd, Bangalore India from Oct 2004 to Feb 2006, then as "Web & Intranet Developer" @ Fosroc International Ltd, Dubai from April 2006 to Sep 2009.
You can catch me on mail@jpy-tech.com or mail@princepy.com. Or on 00971 - 50 - 4284530 

Google Translate

Tag cloud

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

RecentComments

Comment RSS

Google Your Location


C# 3.0 - Language Enhancements

Auto Implemented Properties

        // Method 1 - Old Method
        private int orderID;
        public int OrderID
        {
            get { return orderID; }
            set { orderID = value; }
        }
        // Method 2 - C# 3.0 and Above
        public int OrderID{ get; set; }
        // Method 3 - ReadOnly
        public int OrderID{ get; private set; }

Local Type Inference (var keyword)

        // Part 1
        // LocalType Inference(also known as "Implicitly TypedLocal Variables)
        // Emphasis on Local, as in Local Scope, 
        var first = 7;
        Console.WriteLine("First variable is of type: {0}", first.GetType());

        int third = first;
        Console.WriteLine("Third variable is of type: {0}", third.GetType());
        // Part 2
        // At first glance, you might think var is the same as type Object
        // But Not. Why?  Because Object will take the int and box it into an object.
        // var INFERS the actual type ... no boxing.  It is type-safe.
        object second = 7;
        Console.WriteLine("Second variable is of type: {0}", second.GetType());
        // Part 3
        // The type is INFERED and can be used like any other type 
        // from that point on:

        var fourth = "Hello world!";
        Console.WriteLine("Fourth variable is of type: {0}", fourth.GetType());
        var fifth = fourth;
        Console.WriteLine("Fifth variable is of type: {0}", fifth.GetType()); 

Object Collection Initializers

    class Order
    {
        public int OrderID { get; set; }
        public DateTime OrderDate { get; set; }
        public string CustomerName { get; set; }
        public decimal OrderAmount { get; set; }
        public List OrderItems { get; set; }
        public Address BillingAddress { get; set; }
        public Address ShippingAddress { get; set; }


        public Order()
        {
            OrderItems = new List();
        }


        public Order(int orderID, DateTime orderDate)
        {
            OrderItems = new List();

            OrderID = orderID;
            OrderDate = orderDate;

        }

    }
    ....
    ....
    // Standard technique for initializing the new instance of a class
    Order order1 = new Order();
    order1.OrderID = 122;
    order1.OrderDate = DateTime.Parse("12/7/2007 7:08 AM");
    // Using an overloaded constructor as a technique
    // for initializing a new instance of a class.
    Order order2 = new Order(123, DateTime.Parse("12/7/2007 8:08 AM"));
    // NEW C# 3.0 object initialization syntax
    // This syntax IMPLICITLY calls the default constructor
    Order order3 = new Order { OrderID = 124, OrderDate = DateTime.Parse("12/7/2007 8:10 AM"), CustomerName = "Bob", OrderAmount = 395.00M };
    // This syntax EXPLICITLY calls the default constructor
    // Can you spot the difference?
    Order order4 = new Order() { OrderID = 124, OrderDate = DateTime.Parse("12/7/2007 8:10 AM"), CustomerName = "Bob", OrderAmount = 395.00M };
    // NESTED OBJECT INITIALIZERS ... demonstrates containment
    Order order5 = new Order()
    {
        BillingAddress = new Address { Address1 = "123 E Main", City = "Burbank", State = "IL", Zip = "60459" },
        ShippingAddress = new Address { Address1 = "456 Walnut", City = "Richardson", State = "TX", Zip = "75088" }
    };
    // COLLECTION INITIALIZERS
    // Works with any type that implements ICollection
    Order order6 = new Order()
    {
        OrderItems = new List {
            new OrderItem { OrderItemID = 1, ProductName = "Foo", Quantity = 3 },
            new OrderItem { OrderItemID = 2, ProductName = "Bar", Quantity = 2 },
            new OrderItem { OrderItemID = 3, ProductName = "Bazquirk", Quantity = 1 }
        }
    };

Tags:
Categories: ASP.Net 3.5 | c#
Posted by Admin on Thursday, November 26, 2009 7:11 PM
Permalink | Comments (0) | Post RSSRSS comment feed