/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example5_1.cs illustrates how to declare
classes, object references, and create objects
*/
// declare the Car class
class Car
{
// declare the fields
public string make;
public string model;
public string color;
public int yearBuilt;
// define the methods
public void Start()
{
System.Console.WriteLine(model + " started");
}
public void Stop()
{
System.Console.WriteLine(model + " stopped");
}
}
public class Example5_1
{
public static void Main()
{
// declare a Car object reference named myCar
Car myCar;
// create a Car object, and assign its address to myCar
System.Console.WriteLine("Creating a Car object and assigning " +
"its memory location to myCar");
myCar = new Car();
// assign values to the Car object's fields using myCar
myCar.make = "Toyota";
myCar.model = "MR2";
myCar.color = "black";
myCar.yearBuilt = 1995;
// display the field values using myCar
System.Console.WriteLine("myCar details:");
System.Console.WriteLine("myCar.make = " + myCar.make);
System.Console.WriteLine("myCar.model = " + myCar.model);
System.Console.WriteLine("myCar.color = " + myCar.color);
System.Console.WriteLine("myCar.yearBuilt = " + myCar.yearBuilt);
// call the methods using myCar
myCar.Start();
myCar.Stop();
// declare another Car object reference and
// create another Car object
System.Console.WriteLine("Creating another Car object and " +
"assigning its memory location to redPorsche");
Car redPorsche = new Car();
redPorsche.make = "Porsche";
redPorsche.model = "Boxster";
redPorsche.color = "red";
redPorsche.yearBuilt = 2000;
System.Console.WriteLine("redPorsche is a " + redPorsche.model);
// change the object referenced by the myCar object reference
// to the object referenced by redPorshe
System.Console.WriteLine("Assigning redPorsche to myCar");
myCar = redPorsche;
System.Console.WriteLine("myCar details:");
System.Console.WriteLine("myCar.make = " + myCar.make);
System.Console.WriteLine("myCar.model = " + myCar.model);
System.Console.WriteLine("myCar.color = " + myCar.color);
System.Console.WriteLine("myCar.yearBuilt = " + myCar.yearBuilt);
// assign null to myCar (myCar will no longer reference an object)
myCar = null;
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Declare class and use it
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
using System;
class Rect {
public int width;
public int height;
public Rect(int w, int h) {
width = w;
height = h;
}
public int area() {
return width * height;
}
}
public class UseRect {
public static void Main() {
Rect r1 = new Rect(4, 5);
Rect r2 = new Rect(7, 9);
Console.WriteLine("Area of r1: " + r1.area());
Console.WriteLine("Area of r2: " + r2.area());
}
}
Assign value to class
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// This program will not compile.
class X {
int a;
public X(int i) { a = i; }
}
class Y {
int a;
public Y(int i) { a = i; }
}
public class IncompatibleRef {
public static void Main() {
X x = new X(10);
X x2;
Y y = new Y(5);
x2 = x; // OK, both of same type
x2 = y; // Error, not of same type
}
}
Declaring Class Instances
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_5___Building_Your_Own_Classes
{
public class DeclaringClassInstances
{
static void Main(string[] args)
{
ClassInstantied MyClass = new ClassInstantied();
}
}
class ClassInstantied
{
public void Display()
{
Console.WriteLine("Hello World");
}
}
}
Declaring and Defining Classes
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_5___Building_Your_Own_Classes
{
public class DeclaringandDefiningClasses
{
static private int MyInt = 5;
static public int MyInt2 = 10;
static public int[] MyIntArray;
static private int ObjectCount = 0;
static void Main(string[] args)
{
MyIntArray = new int[10];
ObjectCount++;
}
public static int MyMethod(int myInt)
{
MyInt = MyInt + myInt;
return MyInt;
}
private static long MyLongMethod(ref int myInt)
{
return myInt;
}
}
}
A simple inventory example
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A simple inventory example.
using System;
using System.Collections;
class Inventory {
string name;
double cost;
int onhand;
public Inventory(string n, double c, int h) {
name = n;
cost = c;
onhand = h;
}
public override string ToString() {
return
String.Format("{0,-10}Cost: {1,6:C} On hand: {2}",
name, cost, onhand);
}
}
public class InventoryList {
public static void Main() {
ArrayList inv = new ArrayList();
// Add elements to the list
inv.Add(new Inventory("Pliers", 5.95, 3));
inv.Add(new Inventory("Wrenches", 8.29, 2));
inv.Add(new Inventory("Hammers", 3.50, 4));
inv.Add(new Inventory("Drills", 19.88, 8));
Console.WriteLine("Inventory list:");
foreach(Inventory i in inv) {
Console.WriteLine(" " + i);
}
}
}
Using Initializers
public class Product {
public string make = "Ford";
public string model = "T";
public string color; // default value of null
public int yearBuilt = 1910;
public void Start() {
System.Console.WriteLine(model + " started");
}
public void Stop() {
System.Console.WriteLine(model + " stopped");
}
}
class MainClass {
public static void Main() {
Product myProduct = new Product();
System.Console.WriteLine("myProduct.make = " + myProduct.make);
System.Console.WriteLine("myProduct.model = " + myProduct.model);
if (myProduct.color == null) {
System.Console.WriteLine("myProduct.color is null");
}
System.Console.WriteLine("myProduct.yearBuilt = " + myProduct.yearBuilt);
}
}