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
attrmust bepublicinsideMyClass!
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyYou 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#
1Introduction
Introduction