C# Class Members

Fields and methods within classes are often referred to as "Class Members":

Example

Create a Car class with three class members: two fields and one method.

// The class
            class MyClass
            {
              // Class members
              string color = "red";        // field
              int maxSpeed = 200;          // field
              public void fullThrottle()   // method
              {
                Console.WriteLine("The car is going as fast as it can!");
              }
            }
            

Fields

In the previous chapter, you learned that the variables within a class are called fields, and that you can access them by creating a class object, and using the dot syntax (.).

The following example will create a Car class item, with the name myObj. Then print the field color value and maxSpeed:

Example
class Car 
            {
              string color = "red";
              int maxSpeed = 200;
            
              static void Main(string[] args)
              {
                Car myObj = new Car();
                Console.WriteLine(myObj.color);
                Console.WriteLine(myObj.maxSpeed);
              }
            }
        
    

You can also leave fields blank, and change them when you create an item:

Example
class Car 
            {
              string color;
              int maxSpeed;
            
              static void Main(string[] args)
              {
                Car myObj = new Car();
                myObj.color = "red";
                myObj.maxSpeed = 200;
                Console.WriteLine(myObj.color);
                Console.WriteLine(myObj.maxSpeed);
              }
            }

This is especially important if you are creating multiple items in one category:

Example
class Car 
            {
              string model;
              string color;
              int year;
            
              static void Main(string[] args)
              {
                Car Ford = new Car();
                Ford.model = "Mustang";
                Ford.color = "red";
                Ford.year = 1969;
            
                Car Opel = new Car();
                Opel.model = "Astra";
                Opel.color = "white";
                Opel.year = 2005;
            
                Console.WriteLine(Ford.model);
                Console.WriteLine(Opel.model);
              }
            }

Object Methods

You learned in Chapter C# Methods that methods are used to perform certain actions.

Methods are usually classical, and describe how a class object behaves.

As fields, you can access paths with a dot syntax. However, note that the path must be public. Also remember that we use the method name followed by two parantheses () and a semicolon ; to call (exit) method:

Example
class Car 
            {
              string color;                 // field
              int maxSpeed;                 // field
              public void fullThrottle()    // method
              {
                Console.WriteLine("The car is going as fast as it can!"); 
              }
            
              static void Main(string[] args)
              {
                Car myObj = new Car();
                myObj.fullThrottle();  // Call the method
              }
            }

Use Multiple Classes

Remember from the last chapter, that we can use more classes to get better organized (one for fields and routes, and one for practice). This is recommended:

prog2.cs

class Car 
            {
              public string model;
              public string color;
              public int year;
              public void fullThrottle()
              {
                Console.WriteLine("The car is going as fast as it can!"); 
              }
            }
            

prog.cs

class Program
            {
              static void Main(string[] args)
              {
                Car Ford = new Car();
                Ford.model = "Mustang";
                Ford.color = "red";
                Ford.year = 1969;
            
                Car Opel = new Car();
                Opel.model = "Astra";
                Opel.color = "white";
                Opel.year = 2005;
            
                Console.WriteLine(Ford.model);
                Console.WriteLine(Opel.model);
              }
            }