Define methods that return a value and accept parameters : Class Method « Language Basics « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Collections Data Structure
3. Components
4. Database ADO.net
5. Development Class
6. Event
7. File Stream
8. GUI Windows Form
9. Language Basics
10. Network
11. Office
12. Regular Expressions
13. Services Event
14. Thread
15. Web Services
16. Windows
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / C Sharp » Language Basics » Class MethodScreenshots 
Define methods that return a value and accept parameters
Define methods that return a value and accept parameters

/*
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* 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.");

  }

}

           
       
Related examples in the same category
1. Class a class methodClass a class method
2. Call class methods 2Call class methods 2
3. Method overloading testMethod overloading test
4. Add a method to BuildingAdd a method to Building
5. A simple example that uses a parameterA simple example that uses a parameter
6. Add a method that takes two argumentsAdd a method that takes two arguments
7. Use a class factoryUse a class factory
8. Return an arrayReturn an array
9. Demonstrate method overloadingDemonstrate method overloading
10. Automatic type conversions can affect overloaded method resolutionAutomatic type conversions can affect 
   overloaded method resolution
11. A simple example of recursionA simple example of recursion
12. Overloading ClassesOverloading Classes
13. C# Classes Member FunctionsC# Classes Member Functions
ww__w_._j_ava___2_s__.__c_o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.