Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Class Objects | Introduction to Object-Oriented Programming (OOP)
C# Beyond Basics

bookClass Objects

An object of a class can be created using the following syntax:

index.cs

index.cs

copy
1
className variableName = new className();

We can access the field of a class using the dot (.) operator:

index.cs

index.cs

copy
1
variableName.fieldName

For-example:

index.cs

index.cs

copy
12345678910111213141516
using System; class Player { public string username; } public class ConsoleApp { public static void Main(string[] args) { Player p1= new Player(); p1.username = "Don"; Console.WriteLine(p1.username); } }

The expression new Player(); is used for creating a new object while Player p1 is used for creating a variable for storing that new object. So simply writing Player p1; will only create an empty variable with no object inside it therefore we have to create a new object and, in turn, store it into a variable: Player p1 = new Player();.

Therefore, if we create an Array of size 10 using the Player class, it won't create 10 objects, rather, it will create an empty array with a capacity to store 10 Player objects. This can be understood by the following example:

index.cs

index.cs

copy
12345678910111213141516
using System; class Player { public string username; } public class ConsoleApp { public static void Main(string[] args) { Player[] players = new Player[10]; players[0].username = "Don"; // Error on this line Console.WriteLine(players[0].username); } }

The error says "Object reference not set to an instance of an object" which simply means that we are trying to access data from an object that is not there - there is no object to access at the index 0.

This can be fixed by manually creating an object at the index 0:

index.cs

index.cs

copy
12345678910111213141516
using System; class Player { public string username; } public class ConsoleApp { public static void Main(string[] args) { Player[] players = new Player[10]; players[0] = new Player(); players[0].username = "Don"; Console.WriteLine(players[0].username); } }
question mark

What is the correct syntax for creating a new object from a class called Car?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain why we need to manually create objects in the array?

What happens if I try to access another index that hasn't been initialized?

Can you show an example of initializing all elements in the array?

Awesome!

Completion rate improved to 2.04

bookClass Objects

Scorri per mostrare il menu

An object of a class can be created using the following syntax:

index.cs

index.cs

copy
1
className variableName = new className();

We can access the field of a class using the dot (.) operator:

index.cs

index.cs

copy
1
variableName.fieldName

For-example:

index.cs

index.cs

copy
12345678910111213141516
using System; class Player { public string username; } public class ConsoleApp { public static void Main(string[] args) { Player p1= new Player(); p1.username = "Don"; Console.WriteLine(p1.username); } }

The expression new Player(); is used for creating a new object while Player p1 is used for creating a variable for storing that new object. So simply writing Player p1; will only create an empty variable with no object inside it therefore we have to create a new object and, in turn, store it into a variable: Player p1 = new Player();.

Therefore, if we create an Array of size 10 using the Player class, it won't create 10 objects, rather, it will create an empty array with a capacity to store 10 Player objects. This can be understood by the following example:

index.cs

index.cs

copy
12345678910111213141516
using System; class Player { public string username; } public class ConsoleApp { public static void Main(string[] args) { Player[] players = new Player[10]; players[0].username = "Don"; // Error on this line Console.WriteLine(players[0].username); } }

The error says "Object reference not set to an instance of an object" which simply means that we are trying to access data from an object that is not there - there is no object to access at the index 0.

This can be fixed by manually creating an object at the index 0:

index.cs

index.cs

copy
12345678910111213141516
using System; class Player { public string username; } public class ConsoleApp { public static void Main(string[] args) { Player[] players = new Player[10]; players[0] = new Player(); players[0].username = "Don"; Console.WriteLine(players[0].username); } }
question mark

What is the correct syntax for creating a new object from a class called Car?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 4
some-alt