4 C# Classes, Methods & User Input

 

This chapter covers

  • Properly using classes in C#
  • Working with methods to give our code functionality
  • Using the console class, its methods, and properties
  • Converting data with the Convert class and its methods
  • Developing a number guesser minigame

Nearing the end of Part 1 – Introduction to the C# Programming Language, we slowly become more familiar with the C# development process. We find new types and shapes of coding language with each chapter that we can use to build our programs. Until now, we learned about the fundamentals of this language. We learned about variables, datatypes, randomness, operators, and conditionals and got pretty good at working in our development environment. Furthermore, we were able to build a complete, scalable project in each chapter, which will not change with this chapter.

This chapter's project, the "Guess the number" minigame, will consist of a random number that needs to be guessed by the user. Each time a user input is given, the code will compare the given number to the random number generated and tell the user if the guessed number is too high, too low, or matched. We can then keep track of the number of tries to compete with others.

This chapter's minigame project will teach us everything about user interactivity as well as introduce us in detail to classes, methods, the console class, and the Convert class. Furthermore, like always, provide us with a project ready to be vastly expanded upon.

4.1 Classes in object-oriented C#

4.1.1 Definition of Class

4.1.2 Object creation

4.1.3 Class Inheritance

4.1.4 A brief introduction to Test Driven Development

4.2 Methods in C#

4.2.1 Defining a method

4.2.2 Static and non-static methods

4.2.3 Calling a method

4.2.4 Method return types

4.3 Expanding on the Console class

4.3.1 The Console class

4.3.2 Console properties

4.3.3 Console methods

4.4 The Convert class

4.4.1 Definition of the Convert class

4.4.2 Outcomes to conversions

4.4.3 Convert methods

4.5 Guess the number

4.5.1 The project

4.5.2 Our Code