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

bookChallenge: Creating Classes

Write code for creating a class called Employee which can store the name, age and salary of an employee. The age will be an integer value while the salary will be a double.

index.cs

index.cs

copy
12345678910111213141516171819
using System; // Write class code below this line // Write class code above this line public class ConsoleApp { public static void Main(string[] args) { Employee e1 = new Employee(); e1.name = "Jenny"; e1.age = 32; e1.salary = 70000.0; Console.WriteLine($"{e1.name} is {e1.age} years old and she earns ${e1.salary} per year."); } }
  1. The syntax of the Class declaration is very similar to that of the Struct declaration, but instead of the keyword Struct, we use Class.
  2. If the code fails to compile, ensure you've used the public keyword before each field in the class.
index.cs

index.cs

copy
1234567891011121314151617181920212223
using System; // Write class code below this line class Employee { public string name; public int age; public double salary; } // Write class code above this line public class ConsoleApp { public static void Main(string[] args) { Employee e1 = new Employee(); e1.name = "Jenny"; e1.age = 32; e1.salary = 70000.0; Console.WriteLine($"{e1.name} is {e1.age} years old and she earns ${e1.salary} per year."); } }
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 2.04

bookChallenge: Creating Classes

Swipe to show menu

Write code for creating a class called Employee which can store the name, age and salary of an employee. The age will be an integer value while the salary will be a double.

index.cs

index.cs

copy
12345678910111213141516171819
using System; // Write class code below this line // Write class code above this line public class ConsoleApp { public static void Main(string[] args) { Employee e1 = new Employee(); e1.name = "Jenny"; e1.age = 32; e1.salary = 70000.0; Console.WriteLine($"{e1.name} is {e1.age} years old and she earns ${e1.salary} per year."); } }
  1. The syntax of the Class declaration is very similar to that of the Struct declaration, but instead of the keyword Struct, we use Class.
  2. If the code fails to compile, ensure you've used the public keyword before each field in the class.
index.cs

index.cs

copy
1234567891011121314151617181920212223
using System; // Write class code below this line class Employee { public string name; public int age; public double salary; } // Write class code above this line public class ConsoleApp { public static void Main(string[] args) { Employee e1 = new Employee(); e1.name = "Jenny"; e1.age = 32; e1.salary = 70000.0; Console.WriteLine($"{e1.name} is {e1.age} years old and she earns ${e1.salary} per year."); } }
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3
some-alt