1、原书 P4-P5 代码using System;public struct Nameprivate string fname, mname, lname;public Name(string first, string middle, string last)fname = first;mname = middle;lname = last;public string firstNamegetreturn fname;setfname = firstName;public string middleNamegetreturn mname;setmname = middleName;public
2、 string lastNamegetreturn lname;setlname = lastName;public override string ToString()return (String.Format(“0 1 2“, fname, mname,lname);public string Initials()return (String.Format(“012“, fname.Substring(0, 1),mname.Substring(0, 1), lname.Substring(0, 1);public class NameTeststatic void Main()Name
3、myName = new Name(“Michael“, “Mason“, “McMillan“);string fullName, inits;fullName = myName.ToString();inits = myName.Initials();Console.WriteLine(“My name is 0.“, fullName);Console.WriteLine(“My initials are 0.“, inits);原书 P5-P6 代码using System;public class IntStructstatic void Main()int num;string s
4、num;Console.Write(“Enter a number: “);snum = Console.ReadLine();num = Int32.Parse(snum);Console.WriteLine(num);原书 P12 代码 1public void Add(Object item)InnerList.Add(item);原书 P12 代码 2public void Remove(Object item) InnerList.Remove(item);原书 P13 代码 1public new int Count() return InnerList.Count;原书 P13
5、代码 2public new void Clear() InnerList.Clear();原书 P13-P14 代码using System;using System.Collections;public class Collection : CollectionBasepublic void Add(Object item) InnerList.Add(item);public void Remove(Object item) InnerList.Remove(item);public new void Clear() InnerList.Clear();public new int Co
6、unt() return InnerList.Count;class chapter1static void Main()Collection names = new Collection();names.Add(“David“);names.Add(“Bernica“);names.Add(“Raymond“);names.Add(“Clayton“);foreach (Object name in names)Console.WriteLine(name);Console.WriteLine(“Number of names: “ + names.Count();names.Remove(
7、“Raymond“);Console.WriteLine(“Number of names: “ + names.Count();names.Clear();Console.WriteLine(“Number of names: “ + names.Count();原书 P15 代码 1static void Swap(ref T val1, ref T val2)T temp;temp = val1;val1 = val2;val2 = temp;原书 P15 代码 2using System;class chapter1static void Main()int num1 = 100;in
8、t num2 = 200;Console.WriteLine(“num1: “ + num1);Console.WriteLine(“num2: “ + num2);Swap(ref num1, ref num2);Console.WriteLine(“num1: “ + num1);Console.WriteLine(“num2: “ + num2);string str1 = “Sam“;string str2 = “Tom“;Console.WriteLine(“String 1: “ + str1);Console.WriteLine(“String 2: “ + str2);Swap
9、(ref str1, ref str2);Console.WriteLine(“String 1: “ + str1);Console.WriteLine(“String 2: “ + str2);static void Swap(ref T val1, ref T val2)T temp;temp = val1;val1 = val2;val2 = temp;原书 P16 代码 1public class NodeT data;Node link;public Node(T data, Node link)this.data = data;this.link = link;原书 P16 代码
10、 2Node node1 = new Node(“Mike“, null);Node node2 = new Node(“Raymond“, node1);原书 P17 代码 1static void DisplayNums(int arr)for (int i = 0; i = arr.GetUpperBound(0); i+)Console.Write(arri + “ “);原书 P17 代码 2DateTime startTime;TimeSpan endTime;startTime = DateTime.Now;endTime = DateTime.Now.Subtract(star
11、tTime);原书 P19 代码 1GC.Collect();原书 P19 代码 2GC.WaitForPendingFinalizers();原书 P19 代码 3TimeSpan startingTime;startingTime = Process.GetCurrentProcess().Threads0.UserProcessorTime;原书 P20 代码 1duration = Process.GetCurrentProcess().Threads0.UserProcessorTime.Subtract(startingTime);原书 P20 代码 2using System;u
12、sing System.Diagnostics;class chapter1static void Main()int nums = new int100000;BuildArray(nums);TimeSpan duration;DisplayNums(nums);DisplayNums(nums);DisplayNums(nums);duration = Process.GetCurrentProcess().TotalProcessorTime;Console.WriteLine(“Time: “ + duration.TotalSeconds);static void BuildArr
13、ay(int arr)for (int i = 0; i = 99999; i+)arri = i;static void DisplayNums(int arr)for (int i = 0; i = arr.GetUpperBound(0); i+)Console.Write(arri + “ “);原书 P21 代码public class TimingTimeSpan startingTime;TimeSpan duration;public Timing()startingTime = new TimeSpan(0);duration = new TimeSpan(0);public
14、 void StopTime()duration =Process.GetCurrentProcess().Threads0.UserProcessorTime.Subtract(startingTime);public void startTime()GC.Collect();GC.WaitForPendingFinalizers();startingTime =Process.GetCurrentProcess().Threads0.UserProcessorTime;public TimeSpan Result()return duration;原书 P22-P23 代码using Sy
15、stem;using System.Diagnostics;using System.Threading;public class Timing TimeSpan duration;public Timing() duration = new TimeSpan(0);public void stopTime() duration = Process.GetCurrentProcess().TotalProcessorTime;public void startTime() GC.Collect();GC.WaitForPendingFinalizers();public TimeSpan Re
16、sult()return duration;class chapter1static void Main()int nums = new int100000;BuildArray(nums);Timing tObj = new Timing();tObj.startTime();DisplayNums(nums);tObj.stopTime();Console.WriteLine(“time (.NET): “ + tObj.Result().TotalSeconds);static void BuildArray(int arr)for (int i = 0; i 100000; i+)arri = i;static void DisplayNums(int arr)for (int i = 0; i = arr.GetUpperBound(0); i+)Console.Write(arri + “ “);原书 P23-2 代码startTime = Process.GetCurrentProcess().Threads0.UserProcessorTime;原书 P23-3 代码tObj.startTime();