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 }
}
};