"most anything that you could do with some variable, you can do with a dynamic variable."
static double SumDouble(double a, double b) { return a + b; }
static decimal SumDecimal(decimal a, decimal b) {return a + b;}
this can now be writen as :
static dynamic Sum1(dynamic a, dynamic b) { return a + b; }
or more generic :
static dynamic Sum2
dynamic ad = a;
dynamic bd = b;
return ad + bd;
}
more:
Dynamically Typed Objects with C# 4.0 (xml example)
2) Named arguments, optional arguments, and default values
By giving method parameters default values.
public class ContactListU can then call the method like this:
{
List<Contact> SearchForContacts(
string name = "any",
int age = -1,
string address = "any") { ... }
static void Main()
{
ContactList list = new ContactList();
var x = list.SearchForContacts(age:26);
}
}
list.SearchForContacts(age:26);
or
list.SearchForContacts(address:"home", name:"sam", age:30);
Geen opmerkingen:
Een reactie posten