C # usage of this

Source: Internet
Author: User

C # usage of this

This article summarizes several usage of this in C #. I believe it will be useful, but how many have you used? The following are my summary of the usage of this. You are welcome to refer to the usage and related code.

This keyword references the current instance of the class and can also be used as the modifier of the first parameter of the extension method. The following is a brief summary of the four usage methods of this.

First, we create two C # classes: User and VIP.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

Using System;

Using System. Collections. Generic;

Using System. Linq;

Using System. Text;

Using System. Threading. Tasks;

 

Namespace AppConsole

{

 

/// <Summary>

/// Common user

/// </Summary>

Public class User

{

/// <Summary>

/// Global variable

/// </Summary>

Dictionary <string, object> dictInfo = null;

 

/// <Summary>

/// Constructor

/// </Summary>

Public User ()

{

DictInfo = new Dictionary <string, object> ();

}

 

/// <Summary>

/// Overload the constructor

/// </Summary>

/// <Param name = "UserID"> </param>

/// <Param name = "UserName"> </param>

Public User (int UserID, string UserName)

{

This. UserName = UserName;

This. ID = UserID;

}

 

/// <Summary>

/// This, [1] usage, Indexer

/// </Summary>

/// <Param name = "name"> </param>

/// <Returns> </returns>

Public object this [string name]

{

Get {return dictInfo [name];}

Set {dictInfo [name] = value ;}

}

 

 

/// <Summary>

/// No.

/// </Summary>

Public int ID {get; set ;}

 

 

/// <Summary>

/// User Name

/// </Summary>

Public string UserName {get; set ;}

 

/// <Summary>

/// This parameter is used as a parameter.

/// </Summary>

Public void Said ()

{

New VIP (). Say (this );

}

 

}

 

/// <Summary>

/// Member

/// </Summary>

Public class VIP: User

{

 

 

/// <Summary>

/// Points

/// </Summary>

Public int integral {get; set ;}

 

/// <Summary>

/// Constructor

/// </Summary>

Public VIP ()

{

ID = 520;

Integral = 1000;

}

 

/// <Summary>

/// This is the [3] usage. this () is used to call a non-argument constructor.

/// </Summary>

/// <Param name = "UserName"> </param>

Public VIP (string UserName)

: This ()

{

This. UserName = UserName;

}

 

/// <Summary>

/// Overload the constructor

/// </Summary>

/// <Param name = "UserID"> </param>

/// <Param name = "UserName"> </param>

Public VIP (int UserID, string UserName)

: Base (UserID, UserName)

{

 

}

 

/// <Summary>

/// Say Method

/// </Summary>

/// <Param name = "user"> </param>

Public void Say ([LCQAttribute] User user)

{

Console. WriteLine (string. Format ("Hi, everyone! My ID is {0}. You can call me {1 }! ", User. ID, user. UserName ));

}

 

}

 

/// <Summary>

/// Static class to extend the User class

/// </Summary>

Public static class Helper

{

 

/// <Summary>

/// Method [4]: this extends the User class

/// </Summary>

/// <Param name = "user"> </param>

Public static void Sing (this User user)

{

Console. WriteLine (string. Format ("Hi, everyone! My ID is {0}. You can call me {1 }! ", User. ID, user. UserName ));

}

}

 

/// <Summary>

/// Feature class: specifies that a feature is only applicable to parameters of methods and methods.

/// </Summary>

[System. AttributeUsage (AttributeTargets. Method | AttributeTargets. Parameter)]

Public class LCQAttribute: System. Attribute

{

 

}

}

This [1] usage, Indexer

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

/// <Summary>

/// Global variable

/// </Summary>

Dictionary <string, object> dictInfo = null;

 

/// <Summary>

/// This, [1] usage, Indexer

/// </Summary>

/// <Param name = "name"> </param>

/// <Returns> </returns>

Public object this [string name]

{

Get {return dictInfo [name];}

Set {dictInfo [name] = value ;}

}

This parameter is used as a parameter.

?

1

2

3

4

5

6

7

/// <Summary>

/// This parameter is used as a parameter.

/// </Summary>

Public void Said ()

{

New VIP (). Say (this );

}

This [3] method, which calls a non-argument constructor through this ()

?

1

2

3

4

5

6

7

8

9

/// <Summary>

/// This is the [3] usage. this () is used to call a non-argument constructor.

/// </Summary>

/// <Param name = "UserName"> </param>

Public VIP (string UserName)

: This ()

{

This. UserName = UserName;

}

This [4] usage: extends the User class

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

/// <Summary>

/// Static class to extend the User class

/// </Summary>

Public static class Helper

{

 

/// <Summary>

/// Method [4]: this extends the User class

/// </Summary>

/// <Param name = "user"> </param>

Public static void Sing (this User user)

{

Console. WriteLine (string. Format ("Hi, everyone! My ID is {0}. You can call me {1 }! ", User. ID, user. UserName ));

}

}

Finally, the console Test

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

/// <Summary>

/// Main program entry

/// </Summary>

/// <Param name = "args"> </param>

Static void Main (string [] args)

{

 

// 0> declare an object

User user = new User ();

User. ID = 1;

User. UserName = "lichaoqiang ";

 

// [1] usage: this is used as the index public object this [string name] {…}

User ["UserID"] = 1;

Console. WriteLine ("[1] usage: this is used as the indexer ");

 

// [2] usage: this is used as the parameter to pass user. Say (this );

Console. WriteLine ("[2] usage: this is used for parameter transfer ");

User. Said ();

 

// [3] usage: this () public VIP: this (){}

VIP vip = new VIP ("yezi ");

Vip. Said ();

Console. WriteLine ("[3] usage: this ()");

 

// [4] usage: this extends the VIP class public static Sing (this User user ){......}

Console. WriteLine ("[4] usage: this extends the VIP class ");

User. Sing ();

 

 

Console. Read ();

 

}

Final Result

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.