A simple example that uses a parameter

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A simple example that uses a parameter.

using System;

class ChkNum {
// Return true if x is prime.
public bool isPrime(int x) {
for(int i=2; i < x/2 + 1; i++) if((x %i) == 0) return false; return true; } } public class ParmDemo { public static void Main() { ChkNum ob = new ChkNum(); for(int i=1; i < 10; i++) if(ob.isPrime(i)) Console.WriteLine(i + " is prime."); else Console.WriteLine(i + " is not prime."); } } [/csharp]

Add a method to Building

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Add a method to Building. 
  
using System;  
  
class Building {   
  public int floors;    // number of floors 
  public int area;      // total square footage of building 
  public int occupants; // number of occupants 
 
  // Display the area per person.  
  public void areaPerPerson() {  
    Console.WriteLine("  " + area / occupants + 
                      " area per person"); 
  }  
}   
 
// Use the areaPerPerson() method. 
public class BuildingDemo2 {   
  public static void Main() {   
    Building house = new Building();   
    Building office = new Building(); 
 
 
    // assign values to fields in house 
    house.occupants = 4;  
    house.area = 2500;  
    house.floors = 2;  
 
    // assign values to fields in office 
    office.occupants = 25;  
    office.area = 4200;  
    office.floors = 3;  
   
 
    Console.WriteLine("house has:
  " + 
                      house.floors + " floors
  " + 
                      house.occupants + " occupants
  " + 
                      house.area + " total area"); 
    house.areaPerPerson(); 
 
    Console.WriteLine(); 
 
    Console.WriteLine("office has:
  " + 
                      office.floors + " floors
  " + 
                      office.occupants + " occupants
  " + 
                      office.area + " total area"); 
    office.areaPerPerson(); 
  }   
}


           
          


Method overloading test

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O&#039;Reilly 
ISBN: 0596003765
*/
 using System;

 namespace MethodOverloading
 {
     public class Time1
     {
         // private member variables
         private int Year;
         private int Month;
         private int Date;
         private int Hour;
         private int Minute;
         private int Second;

         // public accessor methods
         public void DisplayCurrentTime()
         {
             System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
                 Month, Date, Year, Hour, Minute, Second);
         }

         // constructors
         public Time1(System.DateTime dt)
         {
             Year =      dt.Year;
             Month =     dt.Month;
             Date =      dt.Day;
             Hour =      dt.Hour;
             Minute =    dt.Minute;
             Second =    dt.Second;
         }

         public Time1(int Year, int Month, int Date,
             int Hour, int Minute, int Second)
         {
             this.Year =     Year;
             this.Month =    Month;
             this.Date =     Date;
             this.Hour =     Hour;
             this.Minute =   Minute;
             this.Second =   Second;
         }
     }

    public class MethodOverloadingTester
    {
       public void Run()
       {
           System.DateTime currentTime = System.DateTime.Now;

           Time1 time1 = new Time1(currentTime);
           time1.DisplayCurrentTime();

           Time1 time2 = new Time1(2000,11,18,11,03,30);
           time2.DisplayCurrentTime();
       }

       static void Main()
       {
          MethodOverloadingTester t = new MethodOverloadingTester();
          t.Run();
       }
    }
 }

           
          


Call class methods 2

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O&#039;Reilly 
ISBN: 0596003765
*/
 using System;

 public class MyTime1 {
     // private member variables
     int year;
     int month;
     int date;
     int hour;
     int minute;
     int second;

     // public method
     public void DisplayCurrentTime()
     {
         System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
             month, date, year, hour, minute, second);
     }

     // constructor
     public MyTime1(int theYear, int theMonth, int theDate,
         int theHour, int theMinute, int theSecond)
     {
         year = theYear;
         month = theMonth;
         date = theDate;
         hour = theHour;
         minute = theMinute;
         second = theSecond;
     }
     static void Main()
     {
         MyTime1 timeObject = new MyTime1(2005,3,25,9,35,20);
         timeObject.DisplayCurrentTime();
     }

 }



           
          


Class a class method

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O&#039;Reilly 
ISBN: 0596003765
*/

 using System;

 class MyClass
 {
     public void SomeMethod(int firstParam, float secondParam)
     {
         Console.WriteLine(
             "Here are the parameters received: {0}, {1}",
             firstParam, secondParam);
     }

 }

 public class Tester111
 {
     static void Main()
     {
         int howManyPeople = 5;
         float pi = 3.14f;
         MyClass mc = new MyClass();
         mc.SomeMethod(howManyPeople, pi);
     }
 }
           
          


Define methods that return a value and accept parameters

image_pdfimage_print

   

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

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example5_3.cs illustrates how to define methods
  that return a value and accept parameters
*/


// declare the Car class
class Car
{

  public int yearBuilt;
  public double maximumSpeed;

  // the Age() method calculates and returns the
  // age of the car in years
  public int Age(int currentYear)
  {
    int age = currentYear - yearBuilt;
    return age;
  }

  // the Distance() method calculates and returns the
  // distance traveled by the car, given its initial speed,
  // maximum speed, and time for the journey
  // (assuming constant acceleration of the car)
  public double Distance(double initialSpeed, double time)
  {
    return (initialSpeed + maximumSpeed) / 2 * time;
  }

}


public class Example5_3
{

  public static void Main()
  {

    // declare a Car object reference and
    // create a Car object
    System.Console.WriteLine("Creating a Car object and " +
      "assigning its memory location to redPorsche");
    Car redPorsche = new Car();

    // assign values to the fields
    redPorsche.yearBuilt = 2000;
    redPorsche.maximumSpeed = 150;

    // call the methods
    int age = redPorsche.Age(2001);
    System.Console.WriteLine("redPorsche is " + age + " year old.");
    System.Console.WriteLine("redPorsche travels " +
      redPorsche.Distance(31, .25) + " miles.");

  }

}

           
          


Method Attributes

image_pdfimage_print
   
 

using System;
using System.Reflection;
public class TransactionableAttribute : Attribute {
    public TransactionableAttribute() {
    }
}

class SomeClass {
    [Transactionable]
    public void Foo() { }

    public void Bar() { }

    [Transactionable]
    public void Goo() { }
}

class Test {
    [STAThread]
    static void Main(string[] args) {
        Type type = Type.GetType("SomeClass");
        foreach (MethodInfo method in type.GetMethods()) {
            foreach (Attribute attr in
                method.GetCustomAttributes(true)) {
                if (attr is TransactionableAttribute) {
                    Console.WriteLine(method.Name);
                }
            }
        }
    }
}