Parameters and extension methods

Source: Internet
Author: User

Summary of this chapter:
1: real parameters and form parameters;
2: name real parameters and optional real parameters;

3: Extension Method;

4: four types of C # Parameters

4.1: Value Type

4.2: Reference Type

4.3: output type

4.4: array parameters

 

1: real parameters and form parameters
Parameter: "formal parameter" is a parameter used when defining the function name and function body. It is used to receive parameters such as those passed when the function is called.
Real parameters: All called "actual parameters" are parameters that pass the function during the call.
View the Code:

 

Int F (int B)/* B is? Parameter? */{Return B;} Main () {int A = 1; printf ("% d", f (a);/* A is a real? */}

 

2: named real parameters and optional Real Parameters
With named arguments, you do not need to remember or find the order of the parameters in the list of parameters of the called method. You can specify the parameters for each real parameter by parameter name. For example, you can call the function for calculating the body mass index (BMI) in a standard way by sending the real parameters of weight and height according to the sequence defined by the function.
Calculatebmi (123, 64 );
If you do not remember the order of the parameters but know their names, you can send the parameters in any order (send weight or height first.
Calculatebmi (weight: 123, height: 64 );
Calculatebmi (Height: 64, weight: 123 );
The method, constructor, indexer, or delegate Definition specifies whether the parameter is required or optional. Any call must provide real parameters for all required parameters, but the optional parameters can be omitted.
Each optional parameter has a default value as part of its definition. If no real parameter is sent for this parameter, the default value is used. The default value must be a constant.
Public void examplemethod (INT required, string optionalstr = "default string", int optionalint = 10)

The following error occurs when examplemethod is called. The cause is that the third parameter is provided instead of the second parameter.
// Anexample. examplemethod (3, 4 );
However, if you know the name of the third parameter, you can use the named real parameter to complete the task.
Anexample. examplemethod (3, optionalint: 4 );

 

 

3: Expansion Method

The extension method is to add a method to an existing type. Currently, the data type can be either basic data type (such as Int or string) or self-defined class. This is a new feature from 3.0.

1. The extension method is to add a method for the existing type;

2. The extension method is to modify the first parameter of the method by specifying the keyword "this;

3. The extension method must be declared in a static class;

4. The extension method should be called by common object instances;

5. The extension method can contain parameters.

 

Code

 // A static class must be created to include the extension method to be added.
Public static class extensions
{
// The extension method to be added must be a static method
// The parameter list of this method must start with this. The second parameter is the data type to be extended. Here it is the string type to be extended.
// It doesn't matter if the third one is an object name. The name is casual and complies with the naming rules.
// In general, this method is to add a method named testmethod to the string type. This method returns an int value, that is, the length of the object that calls this method.
Public static int testmethod (this string S)
{
Return S. length;
}
}
// Test the extension method class
Class Program
{
Static void main (string [] ARGs)
{
String STR = "Hello extension methods ";
// Call the extension method, which must be called by an object
Int Len = Str. testmethod ();
Console. writeline (LEN );
}
}

Public class student
{
Public String description ()
{
Return "Student .............";
}
Public String description (string name)
{
Return "the student's name is" + name;
}
}
// A static class must be created to include the extension method to be added.
Public static class extensions
{
// The extension method to be added must be a static method
// The parameter list of this method must start with this. The second parameter is the data type to be extended. In this example, the student type must be extended.
// It doesn't matter if the third one is an object name. The name is casual and complies with the naming rules.
// In general, this method is to add a method named testmethod to the student type. This method returns a string value.
Public static string testmethod (this student s)
{
Return S. Description ();
}
// The extension method to be added must be a static method
// The first parameter in the parameter list of this method indicates the class to be extended, and the second parameter indicates the real parameter of this extension method.
// In general, this method is to add a method named testmethod to the student type. This method carries a string-type parameter and returns a string-type value.
Public static string testmethod (this student s, string name)
{
Return S. Description (name );
}
}
// Test the extension method class
Class Program
{
Static void main (string [] ARGs)
{
Student Stu = new student ();
// Call the extension method, which must be called by an object
String MEs = Stu. testmethod ();
Console. writeline (MES );
// Call the extension method with parameters. You only need to pass the second parameter.
// Because the first parameter is only used to indicate which data type is extended
MEs = Stu. testmethod ("name1 ");
Console. writeline (MES );
}
}

 

4: four types of C # Parameters

4.1: Value Type

The so-called value parameter is to use the value to pass a parameter to the method, compile the program to make a copy of the value of the real parameter, and pass this copy to the method, the result is that the called method does not modify the value of the real parameter, ensuring the security of the actual value. When calling the method, if the value type of the parameter type is, make sure that the real parameter type is also the value type data.

It must be emphasized that non-base type parameters are not referenced by values.

4.2: Reference Type

You can use the ref keyword to pass Parameters by reference. when you need to pass back the call method, any changes made to the parameter in the method will be reflected in this variable. If you use the ref keyword, the ref keyword must be explicitly used when the method is defined and called.

4.3: output type

The out keyword is used for reference transfer, which is very tiring with the ref keyword. The difference is that the ref requires that the variable must be initialized before being passed. If the out keyword is used, the out keyword must be explicitly used for method definition and call.

4.4: array parameters

Array parameters are the Params declaration keywords used to specify the parameter method parameters when the number of parameters is variable. After the Params keyword in the method declaration, no other parameters are allowed, and only one Params keyword is allowed in the method declaration.

 

 

From: http://www.cnblogs.com/luminji/archive/2010/09/06/1819327.html

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.