C# Class Members (Fields and Methods)

Fields and methods inside classes are often referred to as "Class Members":
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!");
  }
}

In the previous chapter, you learned that variables inside a class are called fields, 
and that you can access them by creating an object of the class, and by using the dot syntax (.).
The following example will create an object of the Car class, with the 
name myObj. Then we print the value of the 
fields color 
and maxSpeed:
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);
  }
} 
 
 


Try it Yourself »

You can also leave the fields blank, and modify them when creating the object:
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);
  }
} 
 


Try it Yourself »

This is especially useful when creating multiple objects of one class:
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);
  }
} 
 
 
 
 
 
 
 


Try it Yourself »

You learned from the C# Methods chapter that methods are used to perform certain actions.
Methods normally belongs to a class, and they define how an object of a class behaves.
Just like with fields, you can access methods with the dot syntax. However, note that 
the method must be public. And remember that we use the name of the method followed by two parantheses () and a semicolon ; to call (execute) the method:
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
  }
} 
 


Try it Yourself »

Why did we declare the method as public, and not static, like in the examples from the C# Methods Chapter?
The reason is simple: a static method can be accessed without creating an object of the class, while public methods can only be accessed by objects.
Remember from the last chapter, that we can use multiple classes for better organization 
(one for fields and methods, and another one for execution). This is 
recommended:
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!"); 
  }
}

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);
  }
}
Try it Yourself »

The public keyword is called an access modifier, 
which specifies that the fields of Car are accessible for other classes as well, such as 
Program.
You will learn more about Access Modifiers in a later chapter.
Tip: As you continue to read, you will also learn more about other class members, such as constructors and properties.
We just launchedW3Schools videos
Get certifiedby completinga course today!
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
help@w3schools.com
Your message has been sent to W3Schools.