/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example2_2.cs illustrates the use of comments
*/
public class Example2_2
{
public static void Main()
{
// display "Hello World!" on the screen
System.Console.WriteLine("Hello World!");
}
}
Language Basics
The Hello World! Application with XML Comments
//csc /doc:HelloWorld.xml HelloWorld.cs
/// Here is the comment for the HelloWorld class
class HelloWorld
{
/// Here is the comment for the Main method
static void Main()
{
System.Console.WriteLine("Hello World!");
}
}
Using break with nested loops
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Using break with nested loops.
using System;
public class BreakNested {
public static void Main() {
for(int i=0; i<3; i++) { Console.WriteLine("Outer loop count: " + i); Console.Write(" Inner loop count: "); int t = 0; while(t < 100) { if(t == 10) break; // terminate loop if t is 10 Console.Write(t + " "); t++; } Console.WriteLine(); } Console.WriteLine("Loops complete."); } } [/csharp]
Find the smallest factor of a value
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Find the smallest factor of a value.
using System;
public class FindSmallestFactor {
public static void Main() {
int factor = 1;
int num = 1000;
for(int i=2; i < num/2; i++) { if((num%i) == 0) { factor = i; break; // stop loop when factor is found } } Console.WriteLine("Smallest factor is " + factor); } } [/csharp]
the break statement
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example4_13.cs illustrates the use of
the break statement
*/
public class Example4_13
{
public static void Main()
{
int total = 0;
for (int counter = 1; counter <= 10; counter++) { System.Console.WriteLine("counter = " + counter); total += counter; if (counter == 5) { System.Console.WriteLine("break from loop"); break; } } System.Console.WriteLine("total = " + total); } } [/csharp]
demonstrates the flags attribute of an enumeration
using System;
[Flags]
public enum Contribution {
Pension = 0x01,
ProfitSharing = 0x02,
CreditBureau = 0x04,
SavingsPlan = 0x08,
All = Pension | ProfitSharing | CreditBureau | SavingsPlan
}
public class Employee {
private Contribution prop_contributions;
public Contribution contributions {
get {
return prop_contributions;
}
set {
prop_contributions = value;
}
}
}
public class Starter {
public static void Main() {
Employee bob = new Employee();
bob.contributions = Contribution.ProfitSharing | Contribution.CreditBureau;
if ((bob.contributions & Contribution.ProfitSharing)== Contribution.ProfitSharing) {
Console.WriteLine("Bob enrolled in profit sharing");
}
}
}
Illustrates the GetCustomAttributes method
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example17_4.cs illustrates the GetCustomAttributes method
*/
using System;
public class Example17_4
{
public static void Main()
{
// retrieve all attributes of Class1
Console.WriteLine("Class1 attributes: ");
object[] aAttributes = Attribute.GetCustomAttributes(
typeof(Class1));
foreach (object attr in aAttributes)
{
Console.WriteLine(attr);
}
}
}
// declare an attribute named UnitTest
// UnitTest.Written is either true or false
public class UnitTest : Attribute
{
bool bWritten;
public bool Written()
{
return bWritten;
}
public UnitTest(bool Written)
{
bWritten = Written;
}
}
// declare another attribute named LifeCycle
// LifeCycle.Stage returns a string
public class LifeCycle : Attribute
{
string sStage;
public string Stage()
{
return sStage;
}
public LifeCycle(string Stage)
{
sStage = Stage;
}
}
// apply the attribues to a class
[UnitTest(true)]
[LifeCycle("Coding")]
public class Class1
{
}