Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Class Methods | Introduction to Object-Oriented Programming (OOP)
C# Beyond Basics

bookClass Methods

Just like structs, classes can also have methods. The syntax for creating and calling methods is also very similar:

For example, we can create a class called Rectangle with width and height attributes and a method called area, which calculates the area of the rectangle object based on the width and height values:

index.cs

index.cs

copy
1234567
class className { // ... other class code public returnType methodName(datatype param1, datatype param2, ...) { // code } }
index.cs

index.cs

copy
123456789101112131415161718192021222324252627
using System; public class ConsoleApp { class Rectangle { public double width; public double height; public double area() { return width * height; } } public static void Main(string[] args) { Rectangle r1 = new Rectangle(); r1.width = 10; r1.height = 20; Rectangle r2 = new Rectangle(); r2.width = 14.7; r2.height= 17.9; Console.WriteLine($"Area of R1 is {r1.area()}"); Console.WriteLine($"Area of R2 is {r2.area()}"); } }
question mark

Which of the following is a valid method?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 7

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Awesome!

Completion rate improved to 2.04

bookClass Methods

Свайпніть щоб показати меню

Just like structs, classes can also have methods. The syntax for creating and calling methods is also very similar:

For example, we can create a class called Rectangle with width and height attributes and a method called area, which calculates the area of the rectangle object based on the width and height values:

index.cs

index.cs

copy
1234567
class className { // ... other class code public returnType methodName(datatype param1, datatype param2, ...) { // code } }
index.cs

index.cs

copy
123456789101112131415161718192021222324252627
using System; public class ConsoleApp { class Rectangle { public double width; public double height; public double area() { return width * height; } } public static void Main(string[] args) { Rectangle r1 = new Rectangle(); r1.width = 10; r1.height = 20; Rectangle r2 = new Rectangle(); r2.width = 14.7; r2.height= 17.9; Console.WriteLine($"Area of R1 is {r1.area()}"); Console.WriteLine($"Area of R2 is {r2.area()}"); } }
question mark

Which of the following is a valid method?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 7
some-alt