using System;
using System.Collections.Generic;
using System.Text;
class Util {
public static int Sum(params int[] paramList) {
if (paramList == null) {
throw new ArgumentException("Util.Sum: null parameter list");
}
if (paramList.Length == 0) {
throw new ArgumentException("Util.Sum: empty parameter list");
}
int sumTotal = 0;
foreach (int i in paramList) {
sumTotal += i;
}
return sumTotal;
}
}
class Program {
static void Entrance() {
Console.WriteLine(Util.Sum(10, 9, 8, 7, 6, 5, 4, 3, 2, 1));
}
static void Main() {
try {
Entrance();
} catch (Exception ex) {
Console.WriteLine("Exception: {0}", ex.Message);
}
}
}
Language Basics
Ref and Out Parameters 2
using System;
class Point
{
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public void GetPoint(out int x, out int y)
{
x = this.x;
y = this.y;
}
int x;
int y;
}
public class RefOutParameters3
{
public static void Main()
{
Point myPoint = new Point(10, 15);
int x;
int y;
myPoint.GetPoint(out x, out y);
Console.WriteLine("myPoint({0}, {1})", x, y);
}
}
C# Ref and Out Parameters
using System;
class Point
{
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public void GetPoint(ref int x, ref int y)
{
x = this.x;
y = this.y;
}
int x;
int y;
}
public class RefOutParameters2
{
public static void Main()
{
Point myPoint = new Point(10, 15);
int x = 0;
int y = 0;
myPoint.GetPoint(ref x, ref y);
Console.WriteLine("myPoint({0}, {1})", x, y);
}
}
Ref and Out Parameters: compiling error
// error
using System;
class Point
{
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
// get both values in one function call
public void GetPoint(ref int x, ref int y)
{
x = this.x;
y = this.y;
}
int x;
int y;
}
public class RefOutParameters {
public static void Main()
{
Point myPoint = new Point(10, 15);
int x;
int y;
// illegal
myPoint.GetPoint(ref x, ref y);
Console.WriteLine("myPoint({0}, {1})", x, y);
}
}
Pass value by reference with read only value
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
namespace PassByRef
{
public class Time
{
// private member variables
private int Year;
private int Month;
private int Date;
private int Hour;
private int Minute;
private int Second;
// Property (read only)
public int GetHour()
{
return Hour;
}
// public accessor methods
public void DisplayCurrentTime()
{
System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
Month, Date, Year, Hour, Minute, Second);
}
// takes references to ints
public void GetTime(ref int h, ref int m, ref int s)
{
h = Hour;
m = Minute;
s = Second;
}
// constructor
public Time(System.DateTime dt)
{
Year = dt.Year;
Month = dt.Month;
Date = dt.Day;
Hour = dt.Hour;
Minute = dt.Minute;
Second = dt.Second;
}
}
public class TesterPassByRef
{
public void Run()
{
System.DateTime currentTime = System.DateTime.Now;
Time t = new Time(currentTime);
t.DisplayCurrentTime();
int theHour = 0;
int theMinute = 0;
int theSecond = 0;
// pass the ints by reference
t.GetTime(ref theHour, ref theMinute, ref theSecond);
System.Console.WriteLine("Current time: {0}:{1}:{2}",
theHour, theMinute, theSecond);
}
static void Main()
{
TesterPassByRef t = new TesterPassByRef();
t.Run();
}
}
}
Pass value by reference
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
namespace PassByRef
{
public class Time3
{
// private member variables
private int Year;
private int Month;
private int Date;
private int Hour;
private int Minute;
private int Second;
// Property (read only)
public int GetHour()
{
return Hour;
}
// public accessor methods
public void DisplayCurrentTime()
{
System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
Month, Date, Year, Hour, Minute, Second);
}
public void GetTime(int h, int m, int s)
{
h = Hour;
m = Minute;
s = Second;
}
// constructor
public Time3(System.DateTime dt)
{
Year = dt.Year;
Month = dt.Month;
Date = dt.Day;
Hour = dt.Hour;
Minute = dt.Minute;
Second = dt.Second;
}
}
public class PassByRefTester
{
public void Run()
{
System.DateTime currentTime = System.DateTime.Now;
Time3 t = new Time3(currentTime);
t.DisplayCurrentTime();
int theHour = 0;
int theMinute = 0;
int theSecond = 0;
t.GetTime(theHour, theMinute, theSecond);
System.Console.WriteLine("Current time: {0}:{1}:{2}",
theHour, theMinute, theSecond);
}
static void Main()
{
PassByRefTester t = new PassByRefTester();
t.Run();
}
}
}
Illustrates the use of out parameters
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example5_8.cs illustrates the use of out parameters
*/
// declare the MyMath class
class MyMath
{
// the SinAndCos() method returns the sin and cos values for
// a given angle (in radians)
public void SinAndCos(double angle, out double sin, out double cos) {
sin = System.Math.Sin(angle);
cos = System.Math.Cos(angle);
}
}
public class Example5_8
{
public static void Main()
{
// declare and set the angle in radians
double angle = System.Math.PI / 2;
// declare the variables that will be used as out paramters
double sin;
double cos;
// create a MyMath object
MyMath myMath = new MyMath();
// get the sin and cos values from the SinAndCos() method
myMath.SinAndCos(angle, out sin, out cos);
// display sin and cos
System.Console.WriteLine("sin = " + sin + ", cos = " + cos);
}
}