1. Trang chủ
  2. » Giáo án - Bài giảng

C Language Enhancements

10 71 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 570 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 1

C# Language Enhancements

Ebook: Beginning Visual C# 2010, part 1, chapter 14

Reference: Vietnamese Video

Trang 2

 Object initializers

 var type

Trang 3

Object 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 4

var 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 5

Anonymous 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 6

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

Named 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 8

Named 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 9

Extension 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 10

Extension 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());

Ngày đăng: 13/05/2014, 11:30

w