Initial commit

parents
Showing with 4855 additions and 0 deletions
TARGETS=ex-10-01.exe ex-10-02.exe ex-10-03.exe ex-10-04.exe ex-10-05.exe ex-10-06.exe ex-10-07.exe ex-10-08.exe ex-10-09.exe
BROKEN=
all: $(TARGETS)
BROKEN: $(BROKEN)
ex-10-01.exe: ex-10-01
csc /debug ex-10-01
ex-10-02.exe: ex-10-02
csc /debug ex-10-02
ex-10-03.exe: ex-10-03
csc /debug ex-10-03
ex-10-04.exe: ex-10-04
csc /debug ex-10-04
ex-10-05.exe: ex-10-05
csc /debug ex-10-05
ex-10-06.exe: ex-10-06
csc /debug ex-10-06
ex-10-07.exe: ex-10-07
csc /debug ex-10-07
ex-10-08.exe: ex-10-08
csc /debug ex-10-08
ex-10-09.exe: ex-10-09
csc /debug ex-10-09
test: all
for %%d in ($(TARGETS)) do %%d
clean:
-del ex-10-01.pdb ex-10-02.pdb ex-10-03.pdb ex-10-04.pdb ex-10-05.pdb ex-10-06.pdb ex-10-07.pdb ex-10-08.pdb ex-10-09.pdb ex-10-01.exe ex-10-02.exe ex-10-03.exe ex-10-04.exe ex-10-05.exe ex-10-06.exe ex-10-07.exe ex-10-08.exe ex-10-09.exe
// Example 10-01: Working with strings
namespace Programming_CSharp
{
using System;
public class StringTester
{
static void Main( )
{
// create some strings to work with
string s1 = "abcd";
string s2 = "ABCD";
string s3 = @"Liberty Associates, Inc.
provides custom .NET development,
on-site Training and Consulting";
int result; // hold the results of comparisons
// compare two strings, case sensitive
result = string.Compare(s1, s2);
Console.WriteLine(
"compare s1: {0}, s2: {1}, result: {2}\n",
s1, s2, result);
// overloaded compare, takes boolean "ignore case"
//(true = ignore case)
result = string.Compare(s1,s2, true);
Console.WriteLine("compare insensitive\n");
Console.WriteLine("s4: {0}, s2: {1}, result: {2}\n",
s1, s2, result);
// concatenation method
string s6 = string.Concat(s1,s2);
Console.WriteLine(
"s6 concatenated from s1 and s2: {0}", s6);
// use the overloaded operator
string s7 = s1 + s2;
Console.WriteLine(
"s7 concatenated from s1 + s2: {0}", s7);
// the string copy method
string s8 = string.Copy(s7);
Console.WriteLine(
"s8 copied from s7: {0}", s8);
// use the overloaded operator
string s9 = s8;
Console.WriteLine("s9 = s8: {0}", s9);
// three ways to compare.
Console.WriteLine(
"\nDoes s9.Equals(s8)?: {0}",
s9.Equals(s8));
Console.WriteLine(
"Does Equals(s9,s8)?: {0}",
string.Equals(s9,s8));
Console.WriteLine(
"Does s9==s8?: {0}", s9 == s8);
// Two useful properties: the index and the length
Console.WriteLine(
"\nString s9 is {0} characters long. ",
s9.Length);
Console.WriteLine(
"The 5th character is {1}\n",
s9.Length, s9[4]);
// test whether a string ends with a set of characters
Console.WriteLine("s3:{0}\nEnds with Training?: {1}\n",
s3,
s3.EndsWith("Training") );
Console.WriteLine(
"Ends with Consulting?: {0}",
s3.EndsWith("Consulting"));
// return the index of the substring
Console.WriteLine(
"\nThe first occurrence of Training ");
Console.WriteLine ("in s3 is {0}\n",
s3.IndexOf("Training"));
// insert the word excellent before "training"
string s10 = s3.Insert(101,"excellent ");
Console.WriteLine("s10: {0}\n",s10);
// you can combine the two as follows:
string s11 = s3.Insert(s3.IndexOf("Training"),
"excellent ");
Console.WriteLine("s11: {0}\n",s11);
}
}
}
// Example 10-02: Using the Substring() method
namespace Programming_CSharp
{
using System;
using System.Text;
public class StringTester
{
static void Main()
{
// create some strings to work with
string s1 = "One Two Three Four";
int ix;
// get the index of the last space
ix=s1.LastIndexOf(" ");
// get the last word.
string s2 = s1.Substring(ix+1);
// set s1 to the substring starting at 0
// and ending at ix (the start of the last word
// thus s1 has one two three
s1 = s1.Substring(0,ix);
// find the last space in s1 (after two)
ix = s1.LastIndexOf(" ");
// set s3 to the substring starting at
// ix, the space after "two" plus one more
// thus s3 = "three"
string s3 = s1.Substring(ix+1);
// reset s1 to the substring starting at 0
// and ending at ix, thus the string "one two"
s1 = s1.Substring(0,ix);
// reset ix to the space between
// "one" and "two"
ix = s1.LastIndexOf(" ");
// set s4 to the substring starting one
// space after ix, thus the substring "two"
string s4 = s1.Substring(ix+1);
// reset s1 to the substring starting at 0
// and ending at ix, thus "one"
s1 = s1.Substring(0,ix);
// set ix to the last space, but there is
// none so ix now = -1
ix = s1.LastIndexOf(" ");
// set s5 to the substring at one past
// the last space. there was no last space
// so this sets s5 to the substring starting
// at zero
string s5 = s1.Substring(ix+1);
Console.WriteLine ("s2: {0}\ns3: {1}",s2,s3);
Console.WriteLine ("s4: {0}\ns5: {1}\n",s4,s5);
Console.WriteLine ("s1: {0}\n",s1);
}
}
}
// Example 10-03: Using the Split() method
namespace Programming_CSharp
{
using System;
using System.Text;
public class StringTester
{
static void Main()
{
// create some strings to work with
string s1 = "One,Two,Three Liberty Associates, Inc.";
// constants for the space and comma characters
const char Space = ' ';
const char Comma = ',';
// array of delimiters to split the sentence with
char[] delimiters = new char[]
{
Space,
Comma
};
string output = "";
int ctr = 1;
// split the string and then iterate over the
// resulting array of strings
foreach (string subString in s1.Split(delimiters))
{
output += ctr++;
output += ": ";
output += subString;
output += "\n";
}
Console.WriteLine(output);
}
}
}
// Example 10-04: Using a StringBuilder
namespace Programming_CSharp
{
using System;
using System.Text;
public class StringTester
{
static void Main()
{
// create some strings to work with
string s1 = "One,Two,Three Liberty Associates, Inc.";
// constants for the space and comma characters
const char Space = ' ';
const char Comma = ',';
// array of delimiters to split the sentence with
char[] delimiters = new char[]
{
Space,
Comma
};
// use a StringBuilder class to build the
// output string
StringBuilder output = new StringBuilder();
int ctr = 1;
// split the string and then iterate over the
// resulting array of strings
foreach (string subString in s1.Split(delimiters))
{
// AppendFormat appends a formatted string
output.AppendFormat("{0}: {1}\n",ctr++,subString);
}
Console.WriteLine(output);
}
}
}
// Example 10-05: Using the Regex class for regular expressions
namespace Programming_CSharp
{
using System;
using System.Text;
using System.Text.RegularExpressions;
public class Tester
{
static void Main( )
{
string s1 =
"One,Two,Three Liberty Associates, Inc.";
Regex theRegex = new Regex(" |, |,");
StringBuilder sBuilder = new StringBuilder( );
int id = 1;
foreach (string subString in theRegex.Split(s1))
{
sBuilder.AppendFormat(
"{0}: {1}\n", id++, subString);
}
Console.WriteLine("{0}", sBuilder);
}
}
}
// Example 10-06: Using static Regex.Split()
namespace Programming_CSharp
{
using System;
using System.Text;
using System.Text.RegularExpressions;
public class Tester
{
static void Main( )
{
string s1 =
"One,Two,Three Liberty Associates, Inc.";
StringBuilder sBuilder = new StringBuilder( );
int id = 1;
foreach (string subStr in Regex.Split(s1," |, |,"))
{
sBuilder.AppendFormat("{0}: {1}\n", id++, subStr);
}
Console.WriteLine("{0}", sBuilder);
}
}
}
// Example 10-07: Using MatchCollection and Match
namespace Programming_CSharp
{
using System;
using System.Text.RegularExpressions;
class Test
{
public static void Main()
{
string string1 = "This is a test string";
// find any nonwhitespace followed by whitespace
Regex theReg = new Regex(@"(\S+)\s");
// get the collection of matches
MatchCollection theMatches =
theReg.Matches(string1);
// iterate through the collection
foreach (Match theMatch in theMatches)
{
Console.WriteLine(
"theMatch.Length: {0}", theMatch.Length);
if (theMatch.Length != 0)
{
Console.WriteLine("theMatch: {0}",
theMatch.ToString());
}
}
}
}
}
// Example 10-08: Using the Group class
namespace Programming_CSharp
{
using System;
using System.Text.RegularExpressions;
class Test
{
public static void Main()
{
string string1 = "04:03:27 127.0.0.0 LibertyAssociates.com";
// group time = one or more digits or colons followed by space
Regex theReg = new Regex(@"(?<time>(\d|\:)+)\s" +
// ip address = one or more digits or dots followed by space
@"(?<ip>(\d|\.)+)\s" +
// site = one or more characters
@"(?<site>\S+)");
// get the collection of matches
MatchCollection theMatches = theReg.Matches(string1);
// iterate through the collection
foreach (Match theMatch in theMatches)
{
if (theMatch.Length != 0)
{
Console.WriteLine("\ntheMatch: {0}",
theMatch.ToString());
Console.WriteLine("time: {0}",
theMatch.Groups["time"]);
Console.WriteLine("ip: {0}",
theMatch.Groups["ip"]);
Console.WriteLine("site: {0}",
theMatch.Groups["site"]);
}
}
}
}
}
// Example 10-09: Examining the capture collection
namespace Programming_CSharp
{
using System;
using System.Text.RegularExpressions;
class Test
{
public static void Main()
{
// the string to parse
// note that names appear in both
// searchable positions
string string1 =
"04:03:27 Jesse 0.0.0.127 Liberty ";
// regular expression which groups company twice
Regex theReg = new Regex(@"(?<time>(\d|\:)+)\s" +
@"(?<company>\S+)\s" +
@"(?<ip>(\d|\.)+)\s" +
@"(?<company>\S+)\s");
// get the collection of matches
MatchCollection theMatches =
theReg.Matches(string1);
// iterate through the collection
foreach (Match theMatch in theMatches)
{
if (theMatch.Length != 0)
{
Console.WriteLine("theMatch: {0}",
theMatch.ToString());
Console.WriteLine("time: {0}",
theMatch.Groups["time"]);
Console.WriteLine("ip: {0}",
theMatch.Groups["ip"]);
Console.WriteLine("Company: {0}",
theMatch.Groups["company"]);
// iterate over the captures collection
// in the company group within the
// groups collection in the match
foreach (Capture cap in
theMatch.Groups["company"].Captures)
{
Console.WriteLine("cap: {0}",cap.ToString());
}
}
}
}
}
}
TARGETS=ex-11-01.exe ex-11-02.exe ex-11-03.exe ex-11-04.exe ex-11-05.exe ex-11-06.exe ex-11-07.exe ex-11-08.exe
BROKEN=
all: $(TARGETS)
BROKEN: $(BROKEN)
ex-11-01.exe: ex-11-01
csc /debug ex-11-01
ex-11-02.exe: ex-11-02
csc /debug ex-11-02
ex-11-03.exe: ex-11-03
csc /debug ex-11-03
ex-11-04.exe: ex-11-04
csc /debug ex-11-04
ex-11-05.exe: ex-11-05
csc /debug ex-11-05
ex-11-06.exe: ex-11-06
csc /debug ex-11-06
ex-11-07.exe: ex-11-07
csc /debug ex-11-07
ex-11-08.exe: ex-11-08
csc /debug ex-11-08
test: all
for %%d in ($(TARGETS)) do %%d
clean:
-del ex-11-01.pdb ex-11-02.pdb ex-11-03.pdb ex-11-04.pdb ex-11-05.pdb ex-11-06.pdb ex-11-07.pdb ex-11-08.pdb ex-11-01.exe ex-11-02.exe ex-11-03.exe ex-11-04.exe ex-11-05.exe ex-11-06.exe ex-11-07.exe ex-11-08.exe
// Example 11-01: Throwing an exception
// Caution: throws an unhandled exception!
namespace Programming_CSharp
{
using System;
public class Test
{
public static void Main()
{
Console.WriteLine("Enter Main...");
Test t = new Test();
t.Func1();
Console.WriteLine("Exit Main...");
}
public void Func1()
{
Console.WriteLine("Enter Func1...");
Func2();
Console.WriteLine("Exit Func1...");
}
public void Func2()
{
Console.WriteLine("Enter Func2...");
throw new System.Exception();
Console.WriteLine("Exit Func2...");
}
}
}
// Example 11-02: Catching an exception
namespace Programming_CSharp
{
using System;
public class Test
{
public static void Main()
{
Console.WriteLine("Enter Main...");
Test t = new Test();
t.Func1();
Console.WriteLine("Exit Main...");
}
public void Func1()
{
Console.WriteLine("Enter Func1...");
Func2();
Console.WriteLine("Exit Func1...");
}
public void Func2()
{
Console.WriteLine("Enter Func2...");
try
{
Console.WriteLine("Entering try block...");
throw new System.Exception();
Console.WriteLine("Exiting try block...");
}
catch
{
Console.WriteLine(
"Exception caught and handled.");
}
Console.WriteLine("Exit Func2...");
}
}
}
// Example 11-03: Catch in a calling function
namespace Programming_CSharp
{
using System;
public class Test
{
public static void Main()
{
Console.WriteLine("Enter Main...");
Test t = new Test();
t.Func1();
Console.WriteLine("Exit Main...");
}
public void Func1()
{
Console.WriteLine("Enter Func1...");
try
{
Console.WriteLine("Entering try block...");
Func2();
Console.WriteLine("Exiting try block...");
}
catch
{
Console.WriteLine(
"Exception caught and handled.");
}
Console.WriteLine("Exit Func1...");
}
public void Func2()
{
Console.WriteLine("Enter Func2...");
throw new System.Exception();
Console.WriteLine("Exit Func2...");
}
}
}
// Example 11-04: Specifying the exception to catch
namespace Programming_CSharp
{
using System;
public class Test
{
public static void Main()
{
Test t = new Test();
t.TestFunc();
}
// try to divide two numbers
// handle possible exceptions
public void TestFunc()
{
try
{
double a = 5;
double b = 0;
Console.WriteLine ("{0} / {1} = {2}",
a, b, DoDivide(a,b));
}
// most derived exception type first
catch (System.DivideByZeroException)
{
Console.WriteLine(
"DivideByZeroException caught!");
}
catch (System.ArithmeticException)
{
Console.WriteLine(
"ArithmeticException caught!");
}
// generic exception type last
catch
{
Console.WriteLine(
"Unknown exception caught");
}
}
// do the division if legal
public double DoDivide(double a, double b)
{
if (b == 0)
throw new System.DivideByZeroException();
if (a == 0)
throw new System.ArithmeticException();
return a/b;
}
}
}
// Example 11-05: Using a finally block
namespace Programming_CSharp
{
using System;
public class Test
{
public static void Main()
{
Test t = new Test();
t.TestFunc();
}
// try to divide two numbers
// handle possible exceptions
public void TestFunc()
{
try
{
Console.WriteLine("Open file here");
double a = 5;
double b = 0;
Console.WriteLine ("{0} / {1} = {2}",
a, b, DoDivide(a,b));
Console.WriteLine (
"This line may or may not print");
}
// most derived exception type first
catch (System.DivideByZeroException)
{
Console.WriteLine(
"DivideByZeroException caught!");
}
catch
{
Console.WriteLine("Unknown exception caught");
}
finally
{
Console.WriteLine ("Close file here.");
}
}
// do the division if legal
public double DoDivide(double a, double b)
{
if (b == 0)
throw new System.DivideByZeroException();
if (a == 0)
throw new System.ArithmeticException();
return a/b;
}
}
}
// Example 11-06: Working with an exception object
namespace Programming_CSharp
{
using System;
public class Test
{
public static void Main()
{
Test t = new Test();
t.TestFunc();
}
// try to divide two numbers
// handle possible exceptions
public void TestFunc()
{
try
{
Console.WriteLine("Open file here");
double a = 12;
double b = 0;
Console.WriteLine ("{0} / {1} = {2}",
a, b, DoDivide(a,b));
Console.WriteLine (
"This line may or may not print");
}
// most derived exception type first
catch (System.DivideByZeroException e)
{
Console.WriteLine(
"\nDivideByZeroException! Msg: {0}",
e.Message);
Console.WriteLine(
"\nHelpLink: {0}", e.HelpLink);
Console.WriteLine(
"\nHere's a stack trace: {0}\n",
e.StackTrace);
}
catch
{
Console.WriteLine(
"Unknown exception caught");
}
finally
{
Console.WriteLine (
"Close file here.");
}
}
// do the division if legal
public double DoDivide(double a, double b)
{
if (b == 0)
{
DivideByZeroException e =
new DivideByZeroException();
e.HelpLink =
"http://www.libertyassociates.com";
throw e;
}
if (a == 0)
throw new ArithmeticException();
return a/b;
}
}
}
// Example 11-07: Creating a custom exception
namespace Programming_CSharp
{
using System;
public class MyCustomException :
System.ApplicationException
{
public MyCustomException(string message):
base(message)
{
}
}
public class Test
{
public static void Main( )
{
Test t = new Test( );
t.TestFunc( );
}
// try to divide two numbers
// handle possible exceptions
public void TestFunc( )
{
try
{
Console.WriteLine("Open file here");
double a = 0;
double b = 5;
Console.WriteLine ("{0} / {1} = {2}",
a, b, DoDivide(a,b));
Console.WriteLine (
"This line may or may not print");
}
// most derived exception type first
catch (System.DivideByZeroException e)
{
Console.WriteLine(
"\nDivideByZeroException! Msg: {0}",