Using the params keyword in C #, the number of method parameters is variable,

Source: Internet
Author: User
Tags natural logarithm

Using the params keyword in C #, the number of method parameters is variable,

I personally think that it is a major advantage of C # syntax to provide the params keyword to Realize Variable Number of method parameters. In the method parameter list, the params keyword is added before parameters of the array type. Generally, codes are more refined when calling methods.

For example, the following code:

[Csharp]View plaincopy
  1. Class Program
  2. {
  3. Static void Main (string [] args)
  4. {
  5. Console. WriteLine (Sum (1 ));
  6. Console. WriteLine (Sum (1, 2, 3 ));
  7. Console. WriteLine (Sum (1, 2, 3, 4, 5 ));
  8. Console. ReadKey ();
  9. }
  10. Private static int Sum (params int [] values)
  11. {
  12. Int sum = 0;
  13. Foreach (int value in values)
  14. Sum + = value;
  15. Return sum;
  16. }
  17. }

 

A Sum method is implemented to receive a group of integers and return their sums. After the parameter values is added with the params keyword, each element of this set of integers can be listed in the real parameter list during the call, which is very convenient.

Note the following when using the params Keyword:

1. params can only be used for one-dimensional arrays. It cannot be used for multi-dimensional arrays or any collection type similar to arrays, such as ArrayList and List <T>.

2. The parameter with the params keyword added must be the last parameter in the parameter list, and only one parameter is allowed in the method declaration.ParamsKeyword.

3. Methods Using the params keyword can be called in four forms:

First, list the elements of the array: Sum (, 3), which is also the most common form;

Second, use the array name as the real parameter: Sum (new int [] {1, 2, 3}), like an array parameter without the params keyword }) or int n = new int [] {1, 2, 3}; Sum (n );;

Third, the parameter with the params keyword can be omitted during the call: Sum (); // return 0; in this way, sometimes one method overload can be defined, but when int Sum () is explicitly defined, the compiler will first call int Sum () instead of Sum (params int [] values ). If the params parameter is omitted, an array with zero element count will still be added in the method, and the efficiency will be slightly checked.

Fourth, params parameters are not omitted, and null is used instead. The efficiency is slightly higher than that of the third type, because the array is not new internally.


How to judge prime numbers using C language

A prime number is a number that cannot be divisible by any integer except 1 and itself. For example, 17 is a prime number because it cannot be 2 ~ Any integer in 16. Therefore, to determine whether an integer m is a prime number, you only need to set m to 2 ~ M-1 no integer can be divisible, m is a prime number.
In addition, the judgment method can be simplified. M does not need to be 2 ~ Remove each integer between the numbers expressed as 1 and 2 ~ √ Remove each integer between m. If m cannot be 2 ~ √ Any integer across m, m must be a prime number. For example, to determine whether 17 is a prime number, you only need to enable 17 to be 2 ~ Since each integer between 4 cannot be divisible, 17 is a prime number. (Cause: If m can be 2 ~ If any integer is divisible between expressed on and off, one of the two factors must be less than or equal to √ m, and the other one must be greater than or equal to √ m. For example, 16 can be divided by 2, 4, 8, 16 = 2*8, 2 less than 4, 8 greater than 4, 16 = 4*4, 4 = √ 16, so only 2 ~ There is no factor between 4)
# Include <stdio. h>
# Include <math. h>
Void main ()
{
Int m, I, k;
Printf ("enter an integer :");
Scanf ("% d", & m );
K = (int) sqrt (m );
For (I = 2; I <= k; I ++)
If (m % I = 0)
Break;
If (I> k)
Printf ("% d is a prime number. \ N ", m );
Else
Printf ("% d is not a prime number. \ N ", m );
}

Returns the arcsin, arccosine, and arctangent functions in C.

Mathematical functions in C Language
The C language provides the following mathematical functions. To use these functions, you must add them to the program file header:
# Include <math. h>
During compilation, the "-lm" parameter must be added to the mathematical function library, for example, "gcc-lm test. c 」.
For the independent variables of the function and the return value types, see the Declaration of the type before the independent variables or functions.
The function is already in "math. h "or other header files have been declared, so you do not need to add a type declaration during use, for example," y = sin (x );」, you do not need to write "y = double sin (double x );」.
Function Description
Double sin (double x)
Sine function value of x
Double cos (double x)
Returns the cosine of x.
Double tan (double x)
Returns the tangent of x.
Double asin (double x)
The inverse sine function value of x sin-1x, the value of x between [-], the return value between [-p/2, p/2]
Double acos (double x)
Returns the inverse cosine function value of cos-1x between [-] and between [-p/2, p/2 ].
Double atan (double x)
Returns the arc tangent function value tan-1x between [-p/2, p/2 ].
Double atan2 (double y, double x)
The arc tangent function value of y/x tan-1 (y/x). The return value is between [-p, p ].
Double sinh (double x)
Returns the hyperbolic sine of x.
Double cosh (double x)
Returns the hyperbolic cosine of x.
Double tanh (double x)
Returns the hyperbolic tangent of x.
Double exp (double x)
X's exponential function ex
Double log (double x)
Natural logarithm ln (x) of x, x> 0
Double log10 (double x)
The base number of x is the logarithm of 10, log10x, x> 0
Double pow (double x, double y)
X to the power of y xy
Double sqrt (double x)
The root number of x √ x
Double ceil (double x)
Minimum integer not less than x (but its type is double)
Double floor (double x)
The maximum integer not greater than x (but its type is double)
Int abs (int x)
Absolute Value of integer x |
Long labs (long x)
Absolute Value of long integer x |
Double fabs (double x)
Absolute value of real number x |

Related Article

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.