Variable params parameters and variable params Parameters

Source: Internet
Author: User

Variable params parameters and variable params Parameters

Namespace params variable parameters
{
Class Program
{
Static void Main (string [] args)
{
Int [] num = {66,99, 55,44 ,};
Test ("Old Wang", num );
Test ("Old Wang", 55); // you can know where the parameters of the method are. The Old Wang should be followed by an array, but the content of the array cannot be modified once it is determined.
// But to change the array content, add a params variable parameter to the method (but the variable content type must be the same as the array type), but this should be placed at the end of the method,
// For example, to add a student ID
Console. ReadKey ();
// Int B = Sum (56, 56, 56 );
// Console. WriteLine (B );
Console. ReadKey ();
}
Public static void Test (string name, params int [] score)
{
Int sum = 0;

For (int I = 0; I <score. Length; I ++)
{
Sum + = score [I];
}
Console. WriteLine ("My name is {0}, my total score is {1}", name, sum );

}
Public static int Sum (params int [] sum)
{
Int a = 0;
For (int I = 0; I <sum. Length; I ++)
{
A + = sum [I];
}
Return;
// Console. WriteLine ();

}
}
}

========================================================== ========================================================== ====================

Sometimes for the convenience of graphs, the sqlhelper file will be used directly for related operations, and the following classes will appear:

public static object ExecuteScalar( string sqlStr, params SqlParameter[] parameters)         {             using (SqlConnection conn = new SqlConnection(connStr))             {                 conn.Open();                 using (SqlCommand cmd = conn.CreateCommand())                 {                     cmd.CommandText = sqlStr;                     cmd.Parameters.AddRange(parameters);                     return cmd.ExecuteScalar();                 }             }         } 

Generally, there are two calling methods:

I. Add Method

SqlParameter sp = new SqlParameter("@name", "Pudding");cmd.Parameters.Add(sp);sp = new SqlParameter("@ID", "1");cmd.Parameters.Add(sp);

This method can only add one SqlParameter at a time. The function of the above Code is to update the name of the field with the ID value equal to 1 to Pudding ).

Ii. AddRange Method

SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@name", "Pudding"), new SqlParameter("@ID", "1") };cmd.Parameters.AddRange(paras);
In my actual operation, cmd. Parameters. AddRange (paras) is not found because Parameters have been added to the ExecuteScalar method. Obviously, the Add method is inconvenient when multiple SqlParameter values are added. In this case, you can use the AddRange method.

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.