/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Demonstrate fixed. using System; class Test { public int num; public Test(int i) { num = i; } } public class FixedCode { // mark Main as unsafe unsafe public static void Main() { Test o = new Test(19); fixed (int* p = &o.num) { // use fixed to put address of o.num into p Console.WriteLine("Initial value of o.num is " + *p); *p = 10; // assign the to count via p Console.WriteLine("New value of o.num is " + *p); } } }
Month: February 2011
Demonstrate pointers and unsafe
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Demonstrate pointers and unsafe. using System; public class UnsafeCode { // mark Main as unsafe unsafe public static void Main() { int count = 99; int* p; // create an int pointer p = &count; // put address of count into p Console.WriteLine("Initial value of count is " + *p); *p = 10; // assign the to count via p Console.WriteLine("New value of count is " + *p); } }
Use params to mark parameter
using System;
class ParamsSample {
static void PrintStrings(params String[] StringArray) {
for (int i = 0; i < StringArray.Length; i++)
Console.Write("{0} ", StringArray[i]);
Console.WriteLine();
}
static void Main(String[] args) {
String names = "K";
PrintStrings("A");
PrintStrings(names, "R", "S");
PrintStrings("R", "E", "C", "S");
}
}
[/csharp]
use the params feature to write functions which accept a variable number of arguments
using System; public class MainClass { public static void Main(string[] args) { int nSum; Console.WriteLine("{0}", SumArgs(out nSum, 1, 2, 3)); int[] nArray = { 4, 5, 6 }; Console.WriteLine("{0}", SumArgs(out nSum, nArray)); } public static int SumArgs(out int nSum, params int[] list) { nSum = 0; foreach (int n in list) { nSum += n; } return nSum; } }
Check the array length for params parameters
using System; public class Employee { public Employee(params string[] _name) { switch (_name.Length) { case 1: name = _name[0]; break; case 2: name = _name[0] + " " + _name[1]; break; default: break; } } public void GetName() { Console.WriteLine(name); } private string name = ""; } public class Employeenel { public static void Main() { Employee bob = new Employee("Bob", "Wilson"); bob.GetName(); } }
Normal parameter and params parameters
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); } }
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); } } }