Dynamic type Regardless of the value that myDynamicVar actually contains, this code will compile If the requested member does not exist, you will get an exception when this code is e
Trang 1C# Language Enhancements
Ebook: Beginning Visual C# 2010, part 1, chapter 14
Reference: Vietnamese Video
Trang 2 Object initializers
var type
Trang 3Object Initializers
public class Student
{
private string id, name, add;
public Student()
{
}
public Student(string id, string name, string add)
{
this.id = id;
this.name = name;
this.add = add;
}
public string StudentID
{
get { return id; }
set { id = value; }
}
public string StudentName
{
get { return name; }
set { name = value; }
}
public string StudentAdd
{
get { return add; }
set { add = value; }
}
}
Slide 3
Student st1 = new Student("9912578", "Nguyen Van An",
"DN");
Student st2 = new Student();
st2.StudentID = "9912578";
st2.StudentName = "Nguyen Van An";
st2.StudentAdd = "DN";
Student st3 = new Student {
StudentID = "9912578", StudentName = "Nguyen Van An", StudentAdd = "DN"
};
Trang 4var type
C# allow to declare a variable with var keyword
You can’t declare a variable using var without initializing the variable at the same time
Example:
var i = 10;
var st = new Student();
var myArray = new[] { 4, 5, 2 };
var <varName> = <value>;
Trang 5Anonymous types
encapsulate a set of read-only properties into a single
object without having to explicitly define a type first
var p = new {ID = "123456", Name = "Join"};
An anonymous type cannot be: a field, a property, an
event, a formal parameter of a method or the return type
of a method
only if all their properties are equal
Slide 5
Trang 6Dynamic type
Regardless of the value that myDynamicVar actually contains, this code will compile
If the requested member does not exist, you will get an
exception when this code is executed
Example:
private dynamic TrungBinh(dynamic s1, dynamic s2)
{
return (s1 + s2) / 2;
}
private void button1_Click(object sender, EventArgs e)
{
dynamic tb = TrungBinh(2, 4);
lblOutput.Text = Convert.ToString( tb);
}
dynamic myDynamicVar;
Trang 7Named and optional method
parameters
Allow to define the parameter as optional in the method
definition by providing a default value that will be used if
none is supplied
Default values must be either literal values, constant values, new object instances, or default value type values
When you use optional values, they must appear at the end
of the list of parameters for a method
Slide 7
Trang 8Named and optional method
parameters
C# 4 introduces named parameters that enable you to
specify whichever parameters you want
This doesn’t require you to do anything in particular in your method definition; it is a technique that you use when you are calling a method
Example:
Student st = new Student(id: "9912578", name: "Nguyen Van An", address:
"Dong Nai");
MyMethod( <param1Name>: <param1Value>,
<paramNName>: <paramNValue>);
Trang 9Extension methods
Extension methods enable you to "add" methods to
existing types without creating a new derived type,
recompiling, or otherwise modifying the original type
Syntax:
Slide 9
public static class ExtensionClass
{
public static <ReturnType><ExtensionMethodName>(
this <TypeToExtend> instance) {
} }
<TypeToExtend> myVar;
// myVar is initialized by code not shown here.
myVar.<ExtensionMethodName>();
Extension methods are defined as static methods but are called by using instance method syntax
Trang 10Extension methods: Example
public static class myString
{
public static bool EqualsIgnoreCase( this string s1, string s2 )
{
return s1.Equals(s2, StringComparison.OrdinalIgnoreCase);
}
}
string s = "Join";
MessageBox.Show(s.EqualsIgnoreCase("join").ToString());