#336 – Declaring and Using a readonly Field

You can make a field in a class read-only by using the readonly modifier when the field is declared.

In the example below, code that creates or uses instances of the Dog class will be able to read the SerialNumber field, but not write to it.

    public class Dog
    {
        public string Name;
        public int Age;
        public readonly string SerialNumber;

        public Dog(string name, int age, string serNumber)
        {
            Name = name;
            Age = age;
            SerialNumber = serNumber;
        }
    }

You can assign a value to a readonly field in only two different places:

  • In the class’ constructor
  • As part of the field’s declaration
        public readonly string BestFriend = "Man";
Advertisements

#310 – Accessibility of Fields in a Class

You can apply access modifiers to fields defined in a class to define their accessibility.  Accessibility dictates what other code is allowed to read and the write the value of a field.

  • public – All code can read/write the field
  • private – Only code in the defining class can read/write the field
  • protected – Code in the defining class or derived classes can read/write the field
  • internal – All code in the defining assembly can read/write the field
  • protected internal – Code in the defining assembly or in derived classes can read/write the field
    public class Dog
    {
        // All code can access
        public string Nickname;

        // Only code in this class can access
        private string genericDogSecretName;

        // Code in this class or subclass can access
        protected int totalBarkCount;

        // Code in same assembly can access
        internal int invokeCount;

        // Code in same assembly or derived classes can access
        protected internal int barkInvokeCount;
    }

#256 – Using Static Fields

Let’s say that we have both instance and static fields defined in a class:

        public string Name;

        public static string Creed;

This allows us read and write the Name property for each instance of the Dog class that we create.

            Dog kirby = new Dog();
            kirby.Name = "Kirby";

            Dog jack = new Dog();
            jack.Name = "Jack";

Notice that each instance of a Dog has its own copy of the Name field, since Name is an instance field.

We can also read/write the static Creed field, using the class name to qualify the field, rather than an instance variable.

            Dog.Creed = "Man's best friend";

There is only one copy of the Creed field, since Creed is a static field.

#237 – Referencing Class Fields from Within One of its Methods

In any of the instance methods for a class, you can reference one of that class’ fields by just using the field name.  The value used will be the value stored in that field, for the object (instance of the class) that was used to invoke the method.

In the example below, we have Name and Age fields in the class.  The PrintNameAndAge method can just reference these fields by name to get their value for the current object.

    public class Dog
    {
        public string Name;
        public int Age;

        public void PrintNameAndAge()
        {
            Console.WriteLine("{0} is {1} yrs old", Name, Age);
        }
    }

We might call this method as follows:

            Dog kirby = new Dog();
            kirby.Name = "Kirby";
            kirby.Age = 15;

            // Prints "Kirby is 15 yrs old"
            kirby.PrintNameAndAge();

#231 – Declaring and Using Instance Fields in a Class

In a class, an instance field is a kind of class member that is a variable declared in the class and having a particular type.  There is one copy of the field for each instance of the class.  You can read and write the value of the field using a reference to the instance of the class.

Fields are declared in a class as follows:

    public class Dog
    {
        public string Name;
        public int Age;
    }

These fields can then be used as follows:

            Dog buster = new Dog();  // Create new instance of Dog
            buster.Name = "Buster";  // Write to Name field
            buster.Age = 3;          // Write to Age field

            Dog kirby = new Dog();   // Another instance of a Dog
            kirby.Name = "Kirby";
            kirby.Age = 13;

            // Reading properties
            int agesAdded = buster.Age + kirby.Age;