Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

C# Tutorial

C# HOME C# Intro C# Get Started C# Syntax C# Output C# Comments C# Variables C# Data Types C# Type Casting C# User Input C# Operators C# Math C# Strings C# Booleans C# If...Else C# Switch C# While Loop C# For Loop C# Break/Continue C# Arrays

C# Methods

C# Methods C# Method Parameters C# Method Overloading C# Scope C# Recursion

C# Classes

C# OOP C# Classes/Objects C# Class Members C# Constructors C# this Keyword C# Access Modifiers C# Properties C# Static C# Inheritance C# Polymorphism C# Abstraction C# Interface C# Enums C# Structs

C# Errors

C# Errors C# Debugging C# Exceptions

C# Data Structures

C# Data Structures C# List C# Dictionary C# HashSet C# Stack C# Queue

C# Advanced

C# Files C# Date and Time C# Nullable Types C# Generics C# Lambda C# LINQ

C# Cert

C# Certificate

C# How To

C# How Tos

C# Examples

C# Examples C# Compiler C# Exercises C# Quiz C# Practice Problems C# Server C# Syllabus C# Study Plan


C# this Keyword


Share

The this Keyword

The this keyword refers to the current object: the object that a constructor or method belongs to.

In the Constructors chapter, the constructor parameter and the field had different names (modelName and model). Many programmers give them the same name, because they describe the same thing. But then C# needs a way to tell them apart.

Look at this constructor. Both the field and the parameter are called model:

Example

class Car
{
  public string model;

  public Car(string model)
  {
    model = model;
  }
}

class Program
{
  static void Main(string[] args)
  {
    Car myCar = new Car("Mustang");
    Console.WriteLine("Model: " + myCar.model);
  }
}

Try it Yourself »

The output is Model: , with no model name. Why?

Inside the constructor, model means the parameter, not the field. So model = model; only gives the parameter its own value. The field is never set, and stays empty.


Use this to Refer to a Field

To refer to the field, write this. in front of it:

Example

class Car
{
  public string model;

  public Car(string model)
  {
    this.model = model;
  }
}

class Program
{
  static void Main(string[] args)
  {
    Car myCar = new Car("Mustang");
    Console.WriteLine("Model: " + myCar.model);
  }
}

Try it Yourself »

this.model is the field of the current object. model is the parameter.

With more fields, use this for each of them:

Example

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

  public Car(string model, string color, int year)
  {
    this.model = model;
    this.color = color;
    this.year = year;
  }
}

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

Try it Yourself »



Call Another Constructor

A class can have more than one constructor, with different parameters. One constructor can call another one with : this(...). This way, you write the code that sets the fields only once:

Example

class Car
{
  public string model;
  public int year;

  public Car(string model, int year)
  {
    this.model = model;
    this.year = year;
  }

  public Car(string model) : this(model, 2020)
  {
  }
}

class Program
{
  static void Main(string[] args)
  {
    Car car1 = new Car("Mustang", 1969);
    Car car2 = new Car("Astra");
    Console.WriteLine(car1.model + " " + car1.year);
    Console.WriteLine(car2.model + " " + car2.year);
  }
}

Try it Yourself »

new Car("Astra") uses the second constructor. It passes the model and the year 2020 on to the first constructor, which sets both fields.

The second constructor has an empty body { }, because the first constructor does all the work.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.