DEV Community

Nwanguma Victor
Nwanguma Victor

Posted on • Updated on

C# for dummies: Classes / Objects, Fields, Access Modifiers, Properties, Constructors

This tutorial assumes you have background or existing knowledge of programming.

C# CLASSES / OBJECTS

A class is a template for objects, and an object is an instance of a class. Objects are entities inside a class.
Example: color is an object of class car.

class Car 
{
  string color = "red";
}
Enter fullscreen mode Exit fullscreen mode

C# FIELDS (variables)

A variable declared directly in a class is often referred to as a field (or attribute).
Example

int myNum = 5;               // Integer (whole number)
double myDoubleNum = 5.99D;  // Floating point number
char myLetter = 'D';         // Character
bool myBool = true;          // Boolean
string myText = "Hello";     // String
Enter fullscreen mode Exit fullscreen mode

C# ACCESS MODIFIERS

Access modifiers are keywords which are used to set the access level/visibility for classes, fields, methods and properties.
Examples
public, private & protected: Are what you would probably only use, unless you reach god level status 😂.

internal: Are for gods.

C# PROPERTIES

Properties are just fields (variables) with access modifiers, the moment you put "public, etc." on a field (variable) it becomes a property.
Example

class Car
{
  public string model;  // public
  private string year;   // private
  string type; // private

  // The `{set; get;}` means you can access and change the property.
  public string model {set; get;} // public
}
Enter fullscreen mode Exit fullscreen mode

C# CONSTRUCTORS

A constructor is a special method that is used to initialize objects.

💡 A constructor is a method that gets called or runs when a class is created.
It can be used to set initial values for fields.
The constructor name must match the class name.
It cannot have a return type (like void or int).

All classes have constructors by default: if you do not create a class constructor yourself, C# creates one for you. However, then you are not able to set initial values for fields.

// Create a Car class
class Car
{
  public string model;  // Create a field

  // Create a class constructor for the Car class
  public Car()
  {
    model = "Mustang"; // Set the initial value for model
  }

  static void Main(string[] args)
  {
    Car Ford = new Car();  // Create an object of the Car Class (this will call the constructor)
    Console.WriteLine(Ford.model);  // Print the value of model
  }
}

// Outputs "Mustang"
Enter fullscreen mode Exit fullscreen mode

CONSTRUCTOR PARAMETERS

Constructors can also take parameters, which is used to initialize fields.

class Car
{
  public string model;
  public string color;
  public int year;

  // Create a class constructor with single or multiple parameters
  public Car(string modelName, string modelColor, int modelYear)
  {
    model = modelName;
    color = modelColor;
    year = modelYear;
  }

  static void Main(string[] args)
  {
    Car Ford = new Car("Mustang", "Red", 1969);
    Console.WriteLine(Ford.color + " " + Ford.year + " " + Ford.model);
  }
}

// Outputs Red 1969 Mustang
Enter fullscreen mode Exit fullscreen mode

REFERENCES

w3schools

Top comments (0)