using System; public class Starter { public static void Main() { Names("F", "F", "B", "A"); Names("F", 1234, 5678, 9876, 4561); Names("F", "C", "D"); } public static void Names(string company,params string[] employees) { Console.WriteLine("{0} employees: ",company); foreach (string employee in employees) { Console.WriteLine(" {0}", employee); } } public static void Names(string company,params int[] emplid) { Console.WriteLine("{0} employees: ",company); foreach (int employee in emplid) { Console.WriteLine(" {0}", employee); } } public static void Names(string company,string empl1, string empl2) { Console.WriteLine("{0} employees: ",company); Console.WriteLine(" {0}", empl1); Console.WriteLine(" {0}", empl2); } }
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Params Array
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); } } }
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(); } } }