Variable in and out a class : Class Definition « Class Interface « C# / C Sharp






Variable in and out a class

Variable in and out a class
 
/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 namespace heap
 {
     public class Dog
     {
         public int weight;
     }

    public class TesterClass
    {
       public void Run()
       {
           // create an integer
           int firstInt = 5;

           // create a second integer
           int secondInt = firstInt;

           // display the two integers
           Console.WriteLine("firstInt: {0} secondInt: {1}",
               firstInt, secondInt);

           // modify the second integer
           secondInt = 7;

           // display the two integers
           Console.WriteLine("firstInt: {0} secondInt: {1}",
               firstInt, secondInt);

           // create a dog
           Dog milo = new Dog();

           // assign a value to weight
           milo.weight = 5;

           // create a second reference to the dog
           Dog fido = milo;

           // display their values
           Console.WriteLine("Milo: {0}, fido: {1}",
               milo.weight, fido.weight);

           // assign a new weight to the second reference
           fido.weight = 7;

           // display the two values
           Console.WriteLine("Milo: {0}, fido: {1}",
               milo.weight, fido.weight);
       }

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

           
         
  








Related examples in the same category

1.simulate a bank account
2.Using Initializers
3.A simple inventory exampleA simple inventory example
4.Declaring and Defining Classes
5.Declaring Class Instances
6.Assign value to classAssign value to class
7.Declare class and use itDeclare class and use it
8.Illustrates how to declare classes, object references, and create objectsIllustrates how to declare classes, object references, and create objects
9.Illustrates how to assign default values to fields using initializersIllustrates how to assign default values to fields using initializers
10.illustrates how to use a 'has a' relationshipillustrates how to use a 'has a' relationship
11.Illustrates nested classesIllustrates nested classes
12.Multiple constructors in a class definitionMultiple constructors in a class definition
13.Demonstrate the use of a nested class to contain dataDemonstrate the use of a nested class to contain data
14.Show name hiding in a derived classShow name hiding in a derived class
15.Illustrates hidingIllustrates hiding
16.Create classCreate class
17.A program that uses the Building classA program that uses the Building class
18.This program creates two Building objectsThis program creates two Building objects
19.Return an objectReturn an object
20.Uses a class from Example16_3a.cs
21.A Simple C# ClassA Simple C# Class
22.Persons Info class
23.Message class
24.Point class