/*
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.");
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Method Attributes
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);
}
}
}
}
}
Build a derived class of Vehicle for trucks
/*
C# A Beginner's Guide
By Schildt
Publisher: Osborne McGraw-Hill
ISBN: 0072133295
*/
/*
Project 8-1
Build a derived class of Vehicle for trucks.
*/
using System;
class Vehicle {
int pri_passengers; // number of passengers
int pri_fuelcap; // fuel capacity in gallons
int pri_mpg; // fuel consumption in miles per gallon
// This is a constructor for Vehicle.
public Vehicle(int p, int f, int m) {
passengers = p;
fuelcap = f;
mpg = m;
}
// Return the range.
public int range() {
return mpg * fuelcap;
}
// Compute fuel needed for a given distance.
public double fuelneeded(int miles) {
return (double) miles / mpg;
}
// Properties
public int passengers {
get { return pri_passengers; }
set { pri_passengers = value; }
}
public int fuelcap {
get { return pri_fuelcap; }
set { pri_fuelcap = value; }
}
public int mpg {
get { return pri_mpg; }
set { pri_mpg = value; }
}
}
// Use Vehicle to create a Truck specialization.
class Truck : Vehicle {
int pri_cargocap; // cargo capacity in pounds
// This is a constructor for Truck.
public Truck(int p, int f, int m, int c) : base(p, f, m)
{
cargocap = c;
}
// Property for cargocap.
public int cargocap {
get { return pri_cargocap; }
set { pri_cargocap = value; }
}
}
public class TruckDemo {
public static void Main() {
// construct some trucks
Truck semi = new Truck(2, 200, 7, 44000);
Truck pickup = new Truck(3, 28, 15, 2000);
double gallons;
int dist = 252;
gallons = semi.fuelneeded(dist);
Console.WriteLine("Semi can carry " + semi.cargocap +
" pounds.");
Console.WriteLine("To go " + dist + " miles semi needs " +
gallons + " gallons of fuel.
");
gallons = pickup.fuelneeded(dist);
Console.WriteLine("Pickup can carry " + pickup.cargocap +
" pounds.");
Console.WriteLine("To go " + dist + " miles pickup needs " +
gallons + " gallons of fuel.");
}
}
a multilevel hierarchy
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
/* In a multilevel hierarchy, the
first override of a virtual method
that is found while moving up the
heirarchy is the one executed. */
using System;
class Base {
// Create virtual method in the base class.
public virtual void who() {
Console.WriteLine("who() in Base");
}
}
class Derived1 : Base {
// Override who() in a derived class.
public override void who() {
Console.WriteLine("who() in Derived1");
}
}
class Derived2 : Derived1 {
// This class also does not override who().
}
class Derived3 : Derived2 {
// This class does not override who().
}
public class NoOverrideDemo2 {
public static void Main() {
Derived3 dOb = new Derived3();
Base baseRef; // a base-class reference
baseRef = dOb;
baseRef.who(); // calls Derived1's who()
}
}
Pass a derived class reference to a base class reference
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Pass a derived class reference to a base class reference.
using System;
class TwoDShape {
double pri_width; // private
double pri_height; // private
// Default constructor.
public TwoDShape() {
width = height = 0.0;
}
// Constructor for TwoDShape.
public TwoDShape(double w, double h) {
width = w;
height = h;
}
// Construct object with equal width and height.
public TwoDShape(double x) {
width = height = x;
}
// Construct object from an object.
public TwoDShape(TwoDShape ob) {
width = ob.width;
height = ob.height;
}
// Properties for width and height.
public double width {
get { return pri_width; }
set { pri_width = value; }
}
public double height {
get { return pri_height; }
set { pri_height = value; }
}
public void showDim() {
Console.WriteLine("Width and height are " +
width + " and " + height);
}
}
// A derived class of TwoDShape for triangles.
class Triangle : TwoDShape {
string style; // private
// A default constructor.
public Triangle() {
style = "null";
}
// Constructor for Triangle.
public Triangle(string s, double w, double h) : base(w, h) {
style = s;
}
// Construct an isosceles triangle.
public Triangle(double x) : base(x) {
style = "isosceles";
}
// Construct an object from an object.
public Triangle(Triangle ob) : base(ob) {
style = ob.style;
}
// Return area of triangle.
public double area() {
return width * height / 2;
}
// Display a triangle's style.
public void showStyle() {
Console.WriteLine("Triangle is " + style);
}
}
public class Shapes7 {
public static void Main() {
Triangle t1 = new Triangle("right", 8.0, 12.0);
// make a copy of t1
Triangle t2 = new Triangle(t1);
Console.WriteLine("Info for t1: ");
t1.showStyle();
t1.showDim();
Console.WriteLine("Area is " + t1.area());
Console.WriteLine();
Console.WriteLine("Info for t2: ");
t2.showStyle();
t2.showDim();
Console.WriteLine("Area is " + t2.area());
}
}
A base class reference can refer to a derived class object
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A base class reference can refer to a derived class object.
using System;
class X {
public int a;
public X(int i) {
a = i;
}
}
class Y : X {
public int b;
public Y(int i, int j) : base(j) {
b = i;
}
}
public class BaseRef {
public static void Main() {
X x = new X(10);
X x2;
Y y = new Y(5, 6);
x2 = x; // OK, both of same type
Console.WriteLine("x2.a: " + x2.a);
x2 = y; // still Ok because Y is derived from X
Console.WriteLine("x2.a: " + x2.a);
// X references know only about X members
x2.a = 19; // OK
// x2.b = 27; // Error, X doesn't have a b member
}
}
A simple class hierarchy
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A simple class hierarchy.
using System;
public class Shapes {
public static void Main() {
Triangle t1 = new Triangle();
Triangle t2 = new Triangle();
t1.width = 4.0;
t1.height = 4.0;
t1.style = "isosceles";
t2.width = 8.0;
t2.height = 12.0;
t2.style = "right";
Console.WriteLine("Info for t1: ");
t1.showStyle();
t1.showDim();
Console.WriteLine("Area is " + t1.area());
Console.WriteLine();
Console.WriteLine("Info for t2: ");
t2.showStyle();
t2.showDim();
Console.WriteLine("Area is " + t2.area());
}
}
// A class for two-dimensional objects.
public class TwoDShape {
public double width;
public double height;
public void showDim() {
Console.WriteLine("Width and height are " +
width + " and " + height);
}
}
// Triangle is derived from TwoDShape.
public class Triangle : TwoDShape {
public string style; // style of triangle
// Return area of triangle.
public double area() {
return width * height / 2;
}
// Display a triangle's style.
public void showStyle() {
Console.WriteLine("Triangle is " + style);
}
}