Menu
Coddy logo textTech

Attributes

Lesson 3 of 12 in Coddy's Basics of Classes and Objects in C# course.

Attributes (also known as fields) are the values inside the classes.

class Point {
	int x = 0;
	int y = 2;
}

In the above example, we create a class named Point with two attributes, x and y with the values 0 and 2 accordingly. 

In order to make the fields accessible outside of the class (we will see more later on) we need to add the public keyword:

class Point {
	public int x = 0;
	public int y = 2;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create a class named Coder with two <strong>public</strong> attributes:

  • A string typed value named name with your name
  • An integer typed value named age with your age

Try it yourself

// Write code here

All lessons in Basics of Classes and Objects in C#