Share
Explore

f23 November 24 Coding Drills

Here are 10 C# programming drills that start from the basics and progress to object-oriented programming.
Each drill will provide you with exercises to gradually improve your skills.

Drill 1: Setting up and Running a "Hello World" Program

Objective: Learn how to set up your development environment and run a simple C# console application.
Tasks:
Install Microsoft Visual Studio Code.
Install the .NET Core SDK.
Create a new folder for your project.
Open a terminal and navigate to your project folder.
Run dotnet new console to create a new console application.
Open the generated Program.cs file in Visual Studio Code.
Write the following code inside the Main method:
Console.WriteLine("Hello World!");
Save the file.
In the terminal, run dotnet run to compile and execute your program.
You should see "Hello World!" printed in the terminal.

Drill 2: Working with Variables

Objective: Understand variable declaration, initialization, and basic data types.
Tasks:
Declare an integer variable named age and assign it a value.
Declare a string variable named name and assign it a value.
Use Console.WriteLine to print out the values of age and name in a sentence.
Experiment by declaring boolean, double, and char variables.

Drill 3: Variable Comparisons

Objective: Learn how to compare variables using comparison operators.
Tasks:
Declare two integer variables and assign them different values.
Compare the variables using the <, >, <=, >=, ==, and != operators.
Print out the results of each comparison using Console.WriteLine.

Drill 4: If-Then Statements

Objective: Understand the control flow with conditional statements.
Tasks:
Create a string variable called password and assign it a value.
Write an if-else statement that checks if password is "letmein".
If the condition is true, print "Access Granted!", otherwise print "Access Denied!".

Drill 5: Loops

Objective: Practice using loops to repeat code execution.
Tasks:
Use a for loop to print numbers from 1 to 10.
Use a while loop to print numbers from 10 down to 1.
Use a do-while loop to prompt the user to enter 'quit' to exit the loop.

Drill 6: Switch-Case Statements

Objective: Learn how to use switch-case statements as an alternative to if-else blocks.
Tasks:
Create a variable dayOfWeek and assign it a value from "Monday" to "Sunday".
Write a switch-case statement to print a different message for each day of the week.

Drill 7: Arrays and Lists

Objective: Understand how to work with collection types such as arrays and lists.
Tasks:
Create an array of integers and initialize it with some numbers.
Loop through the array and print each number.
Create a List of strings and add a few names to it.
Loop through the List and print each name with a greeting.

Drill 8: Methods

Objective: Learn how to define and call methods to organize code better.
Tasks:
Write a method that takes two integers and returns their sum.
Write a method that takes a string and prints it in reverse.
Call both methods with appropriate arguments from the Main method.

Drill 9: Classes and Objects

Objective: Practice defining classes and creating objects.
Tasks:
Define a class called Person with properties for Name and Age.
Create a method within Person that prints a greeting.
In the Main method, create an instance of Person and call the greeting method.

Drill 10: Inheritance and Polymorphism

Objective: Understand the concept of inheritance and polymorphism.
Tasks:
Define a base class Animal with a method Speak.
Define derived classes Dog and Cat that inherit from Animal and override the Speak method.
In the Main method, create instances of Dog and Cat and call the Speak method on each to demonstrate polymorphism.
These drills will guide you through the fundamentals of C# programming and help you grasp the concepts in practice. Remember to compile and run your code after each step to validate your progress.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.