public class Order
{
//Old way
public int Purchase(Product product, Customer customer, bool isResale, string discountCode)
{
}
// New way
public int Purchase(Product product, Customer customer, bool isResale = false, string discountCode = string.Empty)
{
}
}
public class Program
{
static void Main(string[] args)
{
Order newOrder = new Order();
Product item = new Product { ItemCode = "A8901", Price = 50, Qty = 5};
Customer customer = new Customer();
customer.UserName = "R887663";
customer.PreferredCustomer = true;
newOrder.Purchase(item, customer);
}
}
2. Named Parameters
public class Program
{
static void Main(string[] args)
{
Order newOrder = new Order();
Product item = new Product { ItemCode = "A8901", Price = 50, Qty = 5};
Customer customer = new Customer();
customer.UserName = "R887663";
customer.PreferredCustomer = true;
newOrder.Purchase(item, customer, discountCode: 76883987);
}
}
3. Use of dynamic keyword
static class DynamicDemo
{
public static void Execute()
{
var person = new Person { Name = "Alex" };
var company = new Company { Name = "Microsoft" };
DoSomething(person);
DoSomething(company);
}
static void DoSomething(dynamic item)
{
Console.WriteLine(item.Name);
}
}
class Person
{
public string Name { get; set; }
}
class Company
{
public string Name { get; set; }
}
4. Use of DynamicObject
class DynamicObjectSimpleDemo : DynamicObject
{
private Dictionarymembers = new Dictionary ();
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return this.members.TryGetValue(binder.Name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
this.members[binder.Name] = value;
return true;
}
}
public class DynamicObjectXDemo : DynamicObject
{
private XElement element;
public DynamicObjectXDemo (XElement element)
{
this.element = element;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = this.element.Attribute(binder.Name).Value;
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
this.element.SetAttributeValue(binder.Name, value);
return true;
}
}
class program
{
static void main(string[] args)
{
dynamic sample = new DynamicObjectSimpleDemo();
sample.Fname = "Alex";
sample.LName = "Palle";
var xml = XElement.Load("Clients.xml").Elements().Select(x => new DynamicObjectXDemo(x));
foreach (dynamic item in xml)
{
Console.WriteLine(item.FName+ " " + item.LName);
}
}
}
//Clients.xml
No comments:
Post a Comment