Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Defining Classes | Section
C# Object-Oriented Structures

bookDefining Classes

メニューを表示するにはスワイプしてください

The syntax for defining a basic class is the following:

index.cs

index.cs

copy
123456
class nameOfClass { public datatype fieldName1; public datatype fieldName2; public datatype fieldName3; ... }

For example, a class for storing data about houses:

index.cs

index.cs

copy
12345678910111213141516171819
using 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.

Note
Note

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.

question mark

Which keyword is used for defining a class?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  23

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  23
some-alt