C # Value parameters and reference parameters, overloads of methods, foreach, arrays, and use of ref and out

Source: Internet
Author: User

1. Transfer of methods

Value parameter: The copy is passed

Reference parameter: Self-preserving a custom method in which the change parameter of a value is affected by an argument
Ref: the corresponding formal parameter and argument are decorated with ref

Output parameters: Arguments are not assigned, but this parameter must be assigned a value within the custom Method!!! Bring the result of the custom method back to the call point
Out: Both the corresponding formal parameter and the argument are used out modifiers must be assigned in the custom method

Note: If you need to return a parameter, use return
If you need to reverse multiple parameters using ref or Out

TryParse usage: Self-judging conversion successful conversion succeeded reverse true conversion failure returns false

Example: bool f = Int. TryParse (Console.ReadLine (), out N2);
Console.WriteLine (f);

Console.readkey ();
2. Overloading of methods (overload)

Definition: Methods with the same name and different parameter lists in the same class and overloads of the return type-independent method (for example, the WriteLine method has a total of 19 overloads)

Pros: The same method invokes different overloaded methods depending on the type of the parameter list easy to use

Example:
Customizing methods for three overloads
public static int Swap (int a,int b)
{
return a + B;
}
public static double Swap (double a,double b)
{
return a + B;
}
public static string Swap (string a,string b)
{
return a + B;
}
The call in the Main method
int res1 = Swap (1, 2);
Double res2 = Swap (1.5, 2.5);
String res3 = Swap ("Zhang San", "John Doe");
Console.WriteLine ("Results were: \n{0}\n{1}\n{2}", Res1, Res2, Res3);
Console.readkey ();


3. Recursion method
Hanoi: Classic Recursion problem
To invoke itself within a method, you must have the condition of the end of the recursion
Example:
Defines a method, called in main, that outputs from 1 to 100 and
public static int Su (int n) {

if (n==1)
{
return 1;
}
n= Su (n-1) +n;
return n;
}
Called in the main function
Console.WriteLine (Su (100));
Console.readkey ();

Array

1. What is an array:

A, is a data structure
b, size fixed save data is the same data type
C, multiple data Common one name (array name) index starting from 0 to n-1
D, each data in an array is called an array element array name [index]
E, arrays are reference data types

2. The role of arrays: storing data

3. How to use arrays:

Statement
data type [] array name;
such as: int[] arr;
Initialization
Array name = new data type [size];
such as: Arr=new int[5];
Assign value
arr[0]=85;
arr[1]=80;
arr[2]=77;

Use
Length: Number of array elements

Traverse

foreach (element type variable in data or collection name)
Practice:
1. Loop out all planet names
2, the user entered a planetary name, return its position in the solar system
3. Establish a Solar System member array, the first element is the "sun", the contents of the planetary array is copied to the solar coefficient group, and then traverse the output.

Code:
Namespace Day05
{
Class Program
{
static void Main (string[] args)
{
Use of arrays
Statement
Int[] arr1;
Initialization
Arr1=new Int[5];
Declaring and initializing
int[] arr2 = new INT[5];
Assign value
1. Array elements are assigned individually
Arr1[0] = 80;
2. Merge in the first three steps
int[] Arr3 = new Int[5] {1,2,3,4,5};
int[] Arr3 = new int[] {1,2,3,4,5};
Int[] ARR3;
ARR3 = new Int[5] {1,2,3,4,5};
Int[] Arr3 = {1,2,3,4,5};
3. Dynamic Assignment
for (int i = 0; i < 5; i++)
{
Console.Write ("Please enter the scores of the students in {0}:", (i+1));
Arr1[i] = Int. Parse (Console.ReadLine ());
}

Use
Sum
Double sum=0;
for (int i = 0; i < arr1. Length; i++)
{

Sum + = Arr1[i];
}
Average
Double avg = sum/arr1. Length;
Console.WriteLine ("And:" +sum);
Console.WriteLine ("Average value:" +avg);

Maximum Value
Assuming that the first element is assigned a value of the highest fractional group
int max = arr1[0];
for (int i = 1; i < arr1. Length; i++)
{
if (Arr1[i] > Max)
{
max = Arr1[i];
}
}
Console.WriteLine ("Highest score:" +max);
Minimum value

Classroom Exercises
Keyboard input 5 Students ' scores, respectively, calculate the total score, the average score, the highest score, the lowest score

Declaring and initializing an array
Int[] score = new INT[5];
Dynamic receive keyboard input (Assignment) accumulation
Double sum = 0;//Total Score
for (int i = 0; i < score. Length; i++)
{
Console.Write ("Please enter grade {0} Students:", (i + 1));
Score[i] = Int. Parse (Console.ReadLine ());
Sum + = Score[i];
}
Double avg = Sum/score. Length;
Maximum minimum value
int max = score[0];//Highest score
int min = score[0];//minimum score
for (int i = 1; i < score. Length; i++)
{
if (Score[i] > Max)
{
max = Score[i];
}
if (score[i]<min)
{
min = Score[i];
}
}

Console.WriteLine ("Test Result:");
for (int i = 0; i < score. Length; i++)
{
Console.Write (Score[i] + "\ t");
}

Iterating over an array or collection cannot change the value of an array cannot be re-assigned to an iteration variable

foreach (int point in score)
{
Console.Write (point+ "\ t");
}
Console.WriteLine ("\ n \ nthe total score \ t average score \ t max. \ min min");
Console.WriteLine ("{0}\t{1}\t{2}\t{3}", Sum, AVG, Max, min);

Common properties and methods for arrays
Length: Total number of elements on all dimensions of the property array
int[,] score = new Int[3, 5]{{11,12,13,14,15},{21,22,23,24,25},{ 31,32,33,34,35}};
Console.WriteLine ("Number of array elements: {0}", score. Length);
Console.WriteLine (The number of dimensions of the array: {0} ", score. Rank);
Method
Int[] arr = new int[] {67,2,34,2,58};
Sort Ascending sort
Array.Sort (arr);
Console.WriteLine ("Sort:");
foreach (int a in arr)
{
 console.write (a + "\ t");
}
Invert
Array.reverse (arr);
Console.WriteLine ("\ n inversion:");
foreach (int a in arr)
{
Console.Write (a + "\ t");
}
Console.WriteLine ();
Copy
int[] arr2=new int[6];
Array.copy (arr,1,arr2,2,3);
Console.WriteLine ("Copy:");
foreach (int a in arr2)
{
 console.write (a + "\ t");
}
Console.WriteLine ();
Find
int index1 = Array.indexof (arr,2);
int index2 = Array.lastindexof (arr, 2);
Console.WriteLine (INDEX1);
Console.WriteLine (INDEX2);
Empty
Array.clear (Arr,0,arr. Length);
foreach (int a in arr)
{
 console.write (a + "\ t");
}

CopyTo
Arr. CopyTo (arr2,1);
foreach (int a in arr2)
{
Console.Write (A + "\ t");
}

Classroom Exercises

/*
* Create an array of solar system planets that require:
1. Loop out all planet names
2, the user entered a planetary name, return its position in the solar system
3. Establish a Solar System member array, the first element is the "sun", the contents of the planetary array is copied to the solar coefficient group, and then traverse the output.
*/
String[] planets = new string[] {"Venus", "Jupiter", "Mercury", "Mars", "Saturn", "Earth", "Uranus", "Neptune"};
Output

foreach (string s in planets)
{
Console.Write (s+ "");
}
Console.WriteLine ();
Find
Console.Write ("Please enter the name of the planet to look for:");
String planet = Console.ReadLine ();
int index = Array.indexof (planets,planet);
if (Index! =-1)
{
Console.WriteLine ("Location: {0}", index);
}
Else
{
Console.WriteLine ("Does not exist");
}
Copy
String[] Solar=new string[planets. LENGTH+1];
Solar[0] = "Sun";
Planets. CopyTo (solar,1);
Traverse output
foreach (string s in solar)
{
Console.Write (s+ "");
}
Console.WriteLine ();
#endregion
Console.readkey ();
}
}
}


C # Value parameters and reference parameters, overloads of methods, foreach, arrays, and use of ref and out

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.