Menu
Coddy logo textTech

Access Attributes

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

To access attributes in an object, we use:

MyClass myObj = new MyClass();
Console.WriteLine(myObj.attr);

In the above example, we access the attribute called attr (and print it) in the object myObj which is an instance of MyClass class.

Note that attr must be public inside MyClass!

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

You are given a code with a class named SomeClass, in the Main method, create an object for this class and print the two attributes it has line by line.

Try it yourself

using System;

class SomeClass {
    public string foo = "something";
    public string bar = "cool";
}

class Program {
    public static void Main(string[] args) {
        // Write code here
    }
}

All lessons in Basics of Classes and Objects in C#