Example of using the fixed statement

image_pdfimage_print
   
 

using System;

public class Starter {

    private static int[] numbers = { 5, 10, 15, 20, 25, 30 };

    public unsafe static void Main() {
        int count = 0;
        Console.WriteLine(" Pointer Value
");
        fixed (int* pI = numbers) {
            foreach (int a in numbers) {
                Console.WriteLine("{0} : {1}",
                    (int)(pI + count), *((int*)pI + count));
                ++count;
            }
        }
    }
}

    


Expressions 1

image_pdfimage_print
   

/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 * Version: 1
 */
using System;

namespace Client.Chapter_2___Operators_and_Excpressions
{
  public class Expressions
  {
    static void Main(string[] args)
    {
      int   MyInt = 12345;
      int    MyInt2 = 10000;
      int    Sum = 0;
      long   MyLong = MyInt;
      short  MyShort = (short)MyInt;
      

      if (MyInt == MyInt2)
      {
        Sum = MyInt + MyInt2;
      }
      else
      {
        Sum = MyInt - MyInt2;
      }

    }
  }
}

           
          


Expressions to calculate and display the circumference of a circle

image_pdfimage_print

   

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

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example3_1.cs illustrates the use of
  expressions to calculate and display
  the circumference of a circle
*/

public class Example3_1
{

  public static void Main()
  {

    const double Pi = 3.14159;
    double diameter = 2.5;

    // calculate the circumference
    double circumference = Pi * diameter;

    // display the circumference
    System.Console.WriteLine("Circumference = " + circumference);

  }

}

           
          


Demonstrates the use of Environment.Exit() in a command line program

image_pdfimage_print

   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// EnvExit.cs -- Demonstrates the use of Environment.Exit() in
//               a command line program.
//
//               Compile this program with the following command line:
//                   C:>csc EnvExit.cs
namespace nsEnvExit
{
    using System;
    
    public class EnvExit
    {
        static public void Main ()
        {
            FirstFunction ();
            Console.WriteLine ("Application ends");
        }
        static public void FirstFunction()
        {
            SecondFunction ();
            Console.WriteLine ("First Function ends");
        }
        static public void SecondFunction()
        {
            ThirdFunction ();
            Console.WriteLine ("First Function ends");
        }
        static public void ThirdFunction()
        {
            try
            {
                Exception e = new Exception ();
                throw (e);
            }
            catch (Exception)
            {
                Console.WriteLine ("No other lines should print " +
                                   "to the console");
                Environment.Exit (-1);
            }
        }
    }
}



           
          


Passing Exceptions on to the Caller: Caller Inform

image_pdfimage_print
   

/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/

// 04 - Exception HandlingPassing Exceptions on to the CallerCaller Inform
// copyright 2000 Eric Gunnerson
using System;

public class CallerInform
{
    public static void Main()
    {
        Summer summer = new Summer();
        try
        {
            summer.DoAverage();
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: {0}", e);
        }
    }
}
public class Summer
{
    int    sum = 0;
    int    count = 0;
    float    average;
    public void DoAverage()
    {
        try
        {
            average = sum / count;
        }
        catch (DivideByZeroException e)
        {
            // wrap exception in another one,
            // adding additional context.
            throw (new DivideByZeroException(
            "Count is zero in DoAverage()", e));
        }
    }
}

           
          


Passing Exceptions on to the Caller: Caller Confuse

image_pdfimage_print
   

/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/

// 04 - Exception HandlingPassing Exceptions on to the CallerCaller Confuse
// copyright 2000 Eric Gunnerson
using System;

public class CallerConfuse
{
    public static void Main()
    {
        Summer summer = new Summer();
        try
        {
            summer.DoAverage();
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception {0}", e);
        }
    }
}
public class Summer
{
    int    sum = 0;
    int    count = 0;
    float    average;
    public void DoAverage()
    {
        try
        {
            average = sum / count;
        }
        catch (DivideByZeroException e)
        {
            // do some cleanup here
            throw;
        }
    }
}