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

Contenido del Curso

C# Beyond Basics

C# Beyond Basics

1. Additional Structures & File Handling
2. Structs & Enumerators
3. Introduction to Object-Oriented Programming (OOP)
4. OOP Essentials
5. OOP Principles

bookDefining Classes

The syntax for defining a basic class is the following:

cs

index

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

For example, a class for storing data about houses:

cs

index

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.

Using objects is very convenient as we can group related information to form a Class and neatly create multiple instances of that data if needed. Using the House class we can easily store and access data for thousands of houses.

Which keyword is used for defining a class?

Which keyword is used for defining a class?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2
We're sorry to hear that something went wrong. What happened?
some-alt