Demonstrate the use of readonly variables : Variable Definition « Language Basics « C# / C Sharp






Demonstrate the use of readonly variables

Demonstrate the use of readonly variables
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

//
// ReadOnly.cs -- demonstrate the use of readonly variables
//
//                Compile this program with the following command line
//                    C:>csc ReadOnly.cs
//
namespace nsReadOnly
{
    using System;
    
    public class ReadOnly
    {
        static double DegreeFactor = 1;
        static double MilFactor = 0.05625;
        static public void Main ()
        {
            double degrees = 42;
            
            // 1 degree = 17.77778 mils
            double mils = degrees * 17.77778;
            
            // 1 degree = 0.017453 radians
            double radians = degrees * 0.017453;
            clsArea InDegrees = new clsArea (DegreeFactor);
            InDegrees.Angle = degrees;
            InDegrees.Radius = 50;
            Console.WriteLine ("Area of circle is {0,0:F1}", InDegrees.Area);

            // Radians are the default, so you can use the parameterless 
            // constructor
            clsArea InRadians = new clsArea ();
            InRadians.Angle = radians;
            InRadians.Radius = 50;
            Console.WriteLine ("Area of circle is {0,0:F1}", InRadians.Area);

            clsArea InMils = new clsArea (MilFactor);
            InMils.Angle = mils;
            InMils.Radius = 50;
            Console.WriteLine ("Area of circle is {0,0:F1}", InMils.Area);
        }
    }
    class clsArea
    {
        public clsArea ()
        {
        }
        public clsArea (double factor)
        {
            m_Factor = factor / 57.29578;
        }
        private const double pi = 3.14159;
        private const double radian = 57.29578;
        private readonly double m_Factor = 1;
        public double Angle
        {
            get {return (m_Angle);}
            set {m_Angle = value;}
        }
        public double Radius
        {
            get {return (m_Radius);}
            set {m_Radius = value;}
        }
        private double m_Angle;
        private double m_Radius;
        public double Area
        {
            get
            {
               return (m_Radius * m_Radius * pi * m_Angle * m_Factor /  (2 * pi));
            }
        }
    }
}


           
       








Related examples in the same category

1.Declaring a variable.
2.Initializing a variable.
3.Variable default nameVariable default name
4.Heap and Stack Memory
5.Two reference type variables may refer (or point) to the same objectTwo reference type variables may refer (or point)
               to the same object
6.Create a 4-bit type called NybbleCreate a 4-bit type called Nybble
7.Use new with a value typeUse new with a value type
8.Init variableInit variable
9.An attempt to reference an uninitialized variable
10.Illustrates variable scope
11.Uninitialized Values
12.Int, float, double, decimalInt, float, double, decimal
13.Demonstrate dynamic initializationDemonstrate dynamic initialization
14.Demonstrate block scopeDemonstrate block scope
15.Demonstrate lifetime of a variableDemonstrate lifetime of a variable
16.This program attempts to declared a variable in an inner scope
17.Demonstrate castingDemonstrate casting
18.A promotion surpriseA promotion surprise
19.Using casts in an expressionUsing casts in an expression
20.Create an implication operator in C#Create an implication operator in C#
21.declaring a reference type variable and creating an object the variable will reference
22.Definite Assignment and ArraysDefinite Assignment and Arrays
23.Variable Scoping and Definite Assignment:Definite AssignmentVariable Scoping and Definite Assignment:Definite Assignment