Defining Classes
The syntax for defining a basic class is the following:
index.cs
123456class nameOfClass { public datatype fieldName1; public datatype fieldName2; public datatype fieldName3; ... }
For example, a class for storing data about houses:
index.cs
12345678910111213141516171819using System; class House { public string ownerName; public float landArea; public string country; public string state; public string city; public string streetAddress; } public class ConsoleApp { public static void Main(string[] args) { Console.WriteLine ("Nothing here yet."); } }
The above code contains a class that defines a blueprint for a House
object, representing a house that can store the ownerName
, land area, country, state, city and street address.
Using the term public
before every field in the class is a discouraged practice however for the sake of simplicity we will be using the public
keyword for the fields until we learn about Access Modifiers in the next section. The public
keyword makes it possible to access the field data from the classes directly.
Using objects is very convenient as you can group related information to form a Class and neatly create multiple instances of that data if needed. Using the House
class you can easily store and access data for thousands of houses.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you show me an example of how to create an instance of the House class?
How do I access the data stored in a House object?
What are some other examples where using a class would be helpful?
Awesome!
Completion rate improved to 2.04
Defining Classes
Swipe to show menu
The syntax for defining a basic class is the following:
index.cs
123456class nameOfClass { public datatype fieldName1; public datatype fieldName2; public datatype fieldName3; ... }
For example, a class for storing data about houses:
index.cs
12345678910111213141516171819using System; class House { public string ownerName; public float landArea; public string country; public string state; public string city; public string streetAddress; } public class ConsoleApp { public static void Main(string[] args) { Console.WriteLine ("Nothing here yet."); } }
The above code contains a class that defines a blueprint for a House
object, representing a house that can store the ownerName
, land area, country, state, city and street address.
Using the term public
before every field in the class is a discouraged practice however for the sake of simplicity we will be using the public
keyword for the fields until we learn about Access Modifiers in the next section. The public
keyword makes it possible to access the field data from the classes directly.
Using objects is very convenient as you can group related information to form a Class and neatly create multiple instances of that data if needed. Using the House
class you can easily store and access data for thousands of houses.
Thanks for your feedback!