Illustrates the use of a const field


   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example6_2.cs illustrates the use of a const field
*/


// declare the Car class
class Car
{

  // declare a const field
  public const int wheels = 4;

}


public class Example6_2
{

  public static void Main()
  {

    System.Console.WriteLine("Car.wheels = " + Car.wheels);
    // Car.wheels = 5;  // causes compilation error

  }

}

           
          


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 &amp; Contribution.ProfitSharing)== Contribution.ProfitSharing) {
            Console.WriteLine("Bob enrolled in profit sharing");
        }
    }
}