Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
class Program {
static void Main() {
int a = 10;
int b = IncreaseInt( a ); // 對 a 動手腳,將結果存至 b
Console.WriteLine( "a = {0}, b = {1}", a, b ); // Output: a = 10, b = 11
Console.WriteLine( "a " + ( a == b ? "==" : "!=" ) + " b" ); // Output: a != b
Console.WriteLine();
string s1 = "Hello";
string s2 = DoubleString( s1 ); // 對 s1 動手腳,將結果存至 s2
Console.WriteLine( "s1 = {0}, s2 = {1}", s1, s2 ); // Output: s1 = "Hello", s2 = "HelloHello"
Console.WriteLine( "s1 " + ( s1 == s2 ? "==" : "!=" ) + " s2" ); // Output: s1 != s2
Console.WriteLine();
int[] a1 = new int[] { 1, 2, 3, 4, 5 };
int[] a2 = DoubleIntArray( a1 ); // 對 a1 動手腳,將結果存至 a2
WriteArray( "a1", a1 ); // Output: a1 = [2, 4, 6, 8, 10]
WriteArray( "a2", a2 ); // Output: a2 = [2, 4, 6, 8, 10]
Console.WriteLine( "a1 " + ( a1 == a2 ? "==" : "!=" ) + " a2" ); // Output: a1 == a2
Console.WriteLine();
}
static int IncreaseInt( int value ) {
value = value + 1;
return value;
}
static string DoubleString( string s ) {
s = s + s;
return s;
}
static int[] DoubleIntArray( int[] a ) {
for ( int i = 0; i < a.Length; i++ ) {
a[ i ] *= 2;
}
return a;
}
static void WriteArray( string name, int[] a ) {
Console.Write( name + " = [" );
for ( int i = 0; i < a.Length; i++ ) {
if ( i != a.Length - 1 ) {
Console.Write( a[ i ] + ", " );
}
else {
Console.WriteLine( a[ i ] + "]" );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment