Posts

Showing posts from February, 2026

C# String Formatting Challenge: Solution Explained

Practise combining interpolated verbatim strings, Unicode text and escape sequences to display file paths in two languages. Working solution This solution uses exactly two console write calls and retains the required variables. string projectName = "ACME"; string russianMessage = "\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0440\u0443\u0441\u0441\u043a\u0438\u0439 \u0432\u044b\u0432\u043e\u0434"; string englishLocation = $@"c:\Exercise\{projectName}\data.txt"; string russianLocation = $@"c:\Exercise\{projectName}\ru-RU\data.txt"; Console.WriteLine($"View English output:\n {englishLocation}\n"); Console.Write($"{russianMessage}:\n {russianLocation}"); Expected output View English output: c:\Exercise\ACME\data.txt Посмотреть русский вывод: c:\Exercise\ACME\ru-RU\data.txt Why this works $@"..." keeps path backslashes literal and inserts projectName . The backslash before {projectName} separa...

C# Challenge: Format English and Russian Console Output

  Complete the challenge Code challenges reinforce what you learned and build confidence. In this task, you’ll print clear instructions that tell users where your app writes data files. You’re not creating files yet—just formatting console output for approval. You’ll apply character escape sequences , verbatim strings , Unicode , and string interpolation to display instructions in both English and Russian. Challenge: Format and display instructions Start with the two required lines below. You must not change them, but you may add code above or below. Use only Console.WriteLine() or Console.Write() twice total ; any extra spacing or new lines must come from escape sequences, verbatim strings, or interpolation. string projectName = "ACME"; string russianMessage = "\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0440\u0443\u0441\u0441\u043a\u0438\u0439 \u0432\u044b\u0432\u043e\u0434"; Goal output Your code must produce exactly this: View English ou...

C# String Interpolation: Combine Text and Variables

  Exercise – Combine Strings Using String Interpolation While string concatenation is common and easy to use, string interpolation has become the preferred choice when building clear, readable, and compact formatted messages. Interpolation allows you to merge literal text and variable values using a clean template-style syntax. What is string interpolation? String interpolation lets you insert values into a literal string by using a template that contains { } expressions. Each expression evaluates to a value at runtime. To enable interpolation, prefix the string with the $ symbol. Before (concatenation): string message = greeting + " " + firstName + "!"; After (using interpolation): string message = $"{greeting} {firstName}!"; Interpolation reduces clutter and improves readability, especially when combining many values in a single formatted message. Use string interpolation to combine a literal string and a variable Clear any previous code, then enter:...

C# String Concatenation: Combine Text with the + Operator

  Exercise – Combine Strings Using String Concatenation In many applications, you'll often combine data from different sources—literal text, variables, and even numeric values. In this unit, you'll learn how C# uses string concatenation to merge multiple pieces of text into one meaningful string. What is string concatenation? String concatenation simply means combining two or more string values into a single string. Unlike numeric addition, concatenation appends one value to another in sequence. In this exercise, you’ll write C# code to join string values together. Concatenate a literal string and a variable To concatenate text, use the + operator. Replace any previous code in your editor, then enter: string firstName = "Bob"; string message = "Hello " + firstName; Console.WriteLine(message); Output: Hello Bob The literal string "Hello " appears first, and the value stored in firstName is appended immediately after it. Concatenate multiple vari...

C# Escape Sequences, Verbatim Strings and Unicode

Use escape sequences to control console layout, verbatim literals for paths, and Unicode escapes to include text from other writing systems. Newlines, tabs and quotation marks In a regular C# string, a backslash begins an escape sequence. These small examples can be run separately: Console.WriteLine("Hello\nWorld!"); Console.WriteLine("Hello\tWorld!"); Console.WriteLine("Hello \"World\"!"); The first call prints two lines. The second separates the words with a tab, whose width depends on the console. The last prints Hello "World"! . An unescaped quotation mark ends a regular string, so quotation marks that are part of the message must be escaped. Write Windows paths correctly Console.WriteLine("c:\\source\\repos"); Console.WriteLine(@"c:\source\repos"); Both statements display the same path. The regular string doubles each backslash; the verbatim string starts with @ and keeps each backslash literal. In a verbatim s...

C# String Formatting: Module 4 Introduction

Introduction As a developer, you’ll often combine and format literal and variable data in C# to produce a new value—ready to display, save to a file, or transmit over the network. The good news: C# offers several clear and efficient ways to compose and format text. Imagine you’re building a command‑line tool and need to show output that mixes fixed text, values stored in variables, numbers, and text from other languages. How do you format that output so users quickly understand your app’s message? In this module, you’ll use character escape sequences to embed special characters—such as tabs and line feeds—and even render characters from other writing systems like Kanji or Cyrillic . You’ll learn to combine strings using concatenation and create flexible templates using string interpolation . By the end, you’ll confidently control how textual data is presented to your application’s end users. Learning objectives In this module, you will: Create string data that includes tabs, new lin...

C# Student Grades: Format Console Output

Finish the student-grade report by adding a header and spacing the output into columns. This example uses newline and tab escape sequences. Newlines and tab stops \n inserts a newline inside a string. Console.WriteLine() also ends its output with a newline. \t advances to the next tab stop; its visible width depends on the terminal and the preceding text. Complete report program int currentAssignments = 5; int sophia1 = 93; int sophia2 = 87; int sophia3 = 98; int sophia4 = 95; int sophia5 = 100; int nicolas1 = 80; int nicolas2 = 83; int nicolas3 = 82; int nicolas4 = 88; int nicolas5 = 85; int zahirah1 = 84; int zahirah2 = 96; int zahirah3 = 73; int zahirah4 = 85; int zahirah5 = 79; int jeong1 = 90; int jeong2 = 92; int jeong3 = 98; int jeong4 = 100; int jeong5 = 97; int sophiaSum = sophia1 + sophia2 + sophia3 + sophia4 + sophia5; int nicolasSum = nicolas1 + nicolas2 + nicolas3 + nicolas4 + nicolas5; int zahirahSum = zahirah1 + zahirah2 + zahirah3 + zahirah4 + zahirah5; int jeon...

C# Student Grades: Calculate Decimal Averages

Divide each student’s total by the number of assignments to calculate an average. Cast an operand to decimal before dividing so the fractional part is preserved. Why integer division loses the fraction int total = 473; int count = 5; decimal truncated = total / count; decimal average = (decimal)total / count; Console.WriteLine(truncated); Console.WriteLine(average); The first result is 94 because both operands are integers. The second is 94.6. Assigning an already truncated integer result to a decimal variable cannot restore the fraction. Complete student-average example int currentAssignments = 5; int sophia1 = 93; int sophia2 = 87; int sophia3 = 98; int sophia4 = 95; int sophia5 = 100; int nicolas1 = 80; int nicolas2 = 83; int nicolas3 = 82; int nicolas4 = 88; int nicolas5 = 85; int zahirah1 = 84; int zahirah2 = 96; int zahirah3 = 73; int zahirah4 = 85; int zahirah5 = 79; int jeong1 = 90; int jeong2 = 92; int jeong3 = 98; int jeong4 = 100; int jeong5 = 97; int sophiaSum = sophi...

C# Student Grades: Calculate Assignment Totals

Add each student’s five assignment scores and keep the total in an integer variable. This is the calculation step before computing an average. Start with the assignment data The complete example below includes the data from the preparation lesson. Run it in a fresh C# console project; do not paste a second copy of the variable declarations into an existing program. Calculate and display the totals int currentAssignments = 5; int sophia1 = 93; int sophia2 = 87; int sophia3 = 98; int sophia4 = 95; int sophia5 = 100; int nicolas1 = 80; int nicolas2 = 83; int nicolas3 = 82; int nicolas4 = 88; int nicolas5 = 85; int zahirah1 = 84; int zahirah2 = 96; int zahirah3 = 73; int zahirah4 = 85; int zahirah5 = 79; int jeong1 = 90; int jeong2 = 92; int jeong3 = 98; int jeong4 = 100; int jeong5 = 97; int sophiaSum = sophia1 + sophia2 + sophia3 + sophia4 + sophia5; int nicolasSum = nicolas1 + nicolas2 + nicolas3 + nicolas4 + nicolas5; int zahirahSum = zahirah1 + zahirah2 + zahirah3 + zahirah4 + za...

C# Student Grades: Project Setup and Starter Code

Prepare for guided project In this part of the module, you’ll use a browser‑based .NET editor as your code development environment. You’ll write C# code that works with string and numeric variables, performs calculations, and displays results in a console window. Project overview You’re building a Student Grading application that automates the calculation of current grades for each student in a class. The application has the following parameters: You’re given a short list of four students and their five assignment grades. Each assignment grade is an integer value from 0 to 100, where 100 represents a perfect score. Final scores are calculated as the average of the five assignment scores. Your application must perform basic math operations to calculate the final grade for each student. Your application must output each student’s name and final score. The teacher’s grade book currently lists the graded assignments for each student as: Sophia: 93, 87, 98, 95, 100 Nicolas: 80, 83, 82, 88, ...

C# Student Grades Project: Introduction

Introduction Developers perform a set of core tasks almost every day. These tasks include declaring string and numeric variables, assigning and reading values, and performing calculations. Although they seem routine, they’re essential skills. Just as important is the responsibility of presenting the results back to the user. Every developer must master combining these skills to solve real problems. In this scenario, you’re working as a teacher’s assistant at a school. You’ve been asked to build an application that automates student grading. The application will use all of each student’s scored assignments to calculate their current overall grade for the class. The teacher has also given you a specific format that you must follow when you report the student grades. This module walks you through the steps to build a Student Grading application from start to finish. Your code will declare and assign values to variables that represent student data, perform numeric calculations, and then di...

C# Variables Challenge Solution: Console Output

Here’s a review of a working solution for the Module 2 challenge, which shows how to combine literal strings with variables in C#: string name = "Bob"; // Store the user's name int messages = 3; // Number of messages decimal temperature = 34.4m; // Temperature as decimal Console.Write("Hello, "); Console.Write(name); Console.Write("! You have "); Console.Write(messages); Console.Write(" messages in your inbox. The temperature is "); Console.Write(temperature); Console.Write(" celsius."); ✅ Output: Hello, Bob! You have 3 messages in your inbox. The temperature is 34.4 celsius. Key Points from This Solution Variable Initialization string name → stores "Bob" int messages → stores the number 3 decimal temperature → stores 34.4m Using Console.Write() Multiple Console.Write() statements were used to combine text with variables. You could also use Console.WriteLine() stri...

C# Variables Challenge: Display a Personalised Message

In this C# variables exercise , you will learn how to store literal and variable values , combine them, and display them on the console. This is essential for creating beginner-friendly programmes. Step 1: Define Variables For this example, we will use three variables: Name : "Bob" → string Message Count : 3 → int Temperature : 34.4 → decimal string name = "Bob"; // Text data int messageCount = 3; // Whole number decimal temperature = 34.4m; // Decimal number Step 2: Display the Message Using the Console.Write() Combine the literal text with the variables to create a readable message: Console.Write("Hello, " + name + "! "); Console.Write("You have " + messageCount + " messages in your inbox. "); Console.Write("The temperature is " + temperature + " celsius."); ✅ Output: Hello, Bob! You have 3 messages in your inbox. The temperature is 34.4 celsius. Step 3: Use Modern...

C# var Keyword: Implicitly Typed Local Variables

Duration: 6 Minutes Level: Beginner Audience: Students, Developers, Educators Track: .NET Development Introduction C# provides a feature where the compiler can infer a variable’s data type based on its initialized value. This is called implicitly typed local variables , created using the var keyword. This unit teaches you how to declare variables without explicitly specifying their type, allowing the compiler to determine it automatically while ensuring type safety. What Are Implicitly Typed Local Variables? An implicitly typed local variable is declared using var and initialized immediately: var message = "Hello world!"; Here, the compiler infers that message is of type string . The variable cannot change type later: var message = "Hello World!"; message = 10.703m; // ❌ Error: CS0029 In C#, var locks the type at declaration. Unlike some other languages, you cannot later assign a different type. You write var score = 95; Compiler infers The i...

C# Variables: Assign, Read and Update Values

Duration: 10 Minutes Level: Beginner Audience: Students, Developers, Educators Track: .NET Development Introduction Variables are temporary storage containers designed to hold data that can change during program execution. In this exercise, you will practice both writing to (setting) and reading from (getting) variables , which is fundamental for all C# applications. Exercise – Working with Variables 1. Create Your First Variable Clear any previous code and declare a variable: string firstName; firstName = "Bob"; Declaration: Specify the data type ( string ) and variable name ( firstName ) Assignment: Use the = operator to assign a value ( "Bob" ) Assigning a value is also called setting a variable . 2. Common Assignment Errors Right-to-left assignment: The compiler reads the value on the right and stores it in the variable on the left. Reversing this causes an error: "Bob" = firstName; // ❌ Error CS0131 Type mismatch: Variables can...

Declare C# Variables: Types and Naming Rules

Duration: 6 Minutes Level: Beginner Audience: Students, Developers, Educators Track: .NET Development Introduction While literal values are fixed and hard-coded , most applications need to work with data collected at runtime — from users, files, or network sources. To handle this dynamic data , you use variables . A variable is a container that stores a value which can change during program execution . You can assign values, retrieve them, and update them as needed. Variables make your code flexible and allow programs to respond to user input or external data. What is a Variable? A variable stores a specific type of data and provides a human-readable name for a memory location. When you want to store or retrieve a value, you refer to the variable name instead of the raw memory address. Declare a Variable To declare a variable, specify its data type and give it a name : string firstName; This creates a variable of type string called firstName , which can hold ...

C# Literal Values: Strings, Numbers and Booleans

Duration: 10 Minutes Level: Beginner Audience: Students, Developers, Educators Track: .NET Development Introduction In this exercise, you will practice printing different types of literal values in C# and understand why data types are essential. Literal values are constants that never change during program execution. For example, printing a “Hello” message to the console uses a literal string. You’ll also learn how to combine hard-coded values with dynamic data collected from users, using variables of specific types . This exercise will help you confidently create and display literal values of various data types in your applications. What is a Literal Value? A literal value is a fixed constant that does not change. Previously, you printed a literal string to the output console, like "Hello" . This literal is written exactly as it appears, which is why it’s called a literal. Tip: Use the string data type for words, phrases, or any alphanumeric data meant for dis...

C# Literals and Variables: Module 2 Introduction

 Introduction – Store and Retrieve Data Using Literals and Variables in C# (Module 2) Level: Beginner Duration: 3 minutes Audience: Students, Developers, Higher Education Educators, K-12 Educators Track: .NET Development Introduction Working with data is essential in almost every C# application. Sometimes, this data is hard-coded — values that remain constant throughout program execution. For example, a “success” message displayed to users after completing an operation is typically the same every time. These constant values are often called literal values. In practical applications, you’ll often need to combine hard-coded values with user-provided data. To do this effectively, you create variables that can hold data of various types — numeric, alphanumeric, or others. This module guides you through creating literal values and defining variables, assigning them values, retrieving them later in your code, and simplifying your workflow using the compiler’s implicit type inference. By ...

C# Console Challenge Solution: Write vs WriteLine

  Introduction In this module, we review a possible solution for the previous C# challenge where you displayed two messages in the console. Understanding different ways to solve a problem reinforces your coding skills and helps you learn multiple approaches in C#. Example Solution Console.WriteLine("This is the first line."); Console.Write("This is "); Console.Write("the second "); Console.Write("line."); Explanation: Console.WriteLine() → Prints a line and moves to the next line. Console.Write() → Prints text without moving to a new line, allowing multiple segments to form a single line. This combination shows how to control console output precisely. Remember: There are many ways to achieve the same output. The goal is to understand the methods and their differences. Expected Output This is the first line. This is the second line. Conclusion Using Console.Write() together with Console.WriteLine() demonstrates how...

C# Challenge: Display Two Console Messages

  Introduction Code challenges help reinforce what you’ve learnt and build confidence. In this C# challenge, you will write code to display two messages in the console using the techniques learnt in previous modules. Challenge Instructions Open the C# Code Editor in your browser. Delete any existing code in the editor. Write code that produces the following output: This is the first line. This is the second line. Tips for Completing the Challenge You can display a message using Console.  WriteLine() or Console.  Write() . You can use multiple lines of code or split a single message into multiple lines. The goal is to produce the exact output shown above. Don’t worry if your approach differs from others; as long as the output matches, your solution is correct. Example Solution (Optional) Console.WriteLine("This is the first line."); Console.WriteLine("This is the second line."); Tip: Experiment with Console.Write() for printin...

How C# Code Works: Compilation and Syntax

  Introduction To truly understand your C# code, it’s important to step back and consider what a programming language is. Programming languages like C# allow you to write instructions for the computer to execute. These instructions, called source code , are written in a way that humans can understand, while computers need a special process to read them. 1. C# source .cs files contain the instructions you write. 2. Compiler The C# compiler checks the code and creates CIL plus metadata. 3. .NET runtime The JIT compiler translates the needed CIL into native instructions. 4. Execution The processor runs the native instructions and the program produces its result. A typical JIT-based .NET execution path. Ahead-of-time publishing can compile native code before execution. 1️⃣ What is a Programming Language? A programming language lets developers express instructions clearly. While each language has its own syntax, many core concepts are similar across languages. Your source code is ...

Your First C# Program: Hello World and Console Output

  Introduction In this hands-on C# exercise, you will write your first lines of code and print messages to the console. This is a classic beginner exercise in programming: printing the phrase “Hello World!”   Though simple, it introduces you to fundamental concepts of coding and the C# programming language. 1️⃣ Writing Your First Line of Code All software developers start with the traditional Hello World program. Open the C# Code Editor in your browser (no installation required) and type the following: Console.WriteLine("Hello World!"); How it works: Console.WriteLine() prints text to the console and adds a new line at the end. C# is case-sensitive , so Console must start with a capital C . Always use double quotes for strings and a semicolon   ; at the end of the line. Tip: Typing the code yourself helps reinforce memory and understanding, instead of just copying and running. 2️⃣ Running Your Code Click the ▶️ Run  the button in the ...

C# Programming for Beginners: Start Here

  Introduction C# (pronounced “C-sharp”) is a modern programming language that allows you to build a wide range of applications, including: Business Applications – Analyse and process data efficiently. Dynamic Web Applications – Create websites that work in any browser. Games – Develop both 2D and 3D games. Financial and Scientific Applications – Solve complex calculations and simulations. Cloud-Based Applications – Deploy scalable apps in the cloud. Mobile Applications – Build apps for smartphones and tablets. But how do you start writing your own applications? Applications are made up of many lines of code working together to perform tasks. The best way to learn C# is by writing code yourself. Following exercises and solving small coding challenges will help you understand programming concepts quickly and gain hands-on experience. What You Will Learn in This Module By completing this lesson, you will: Write your first lines of C# code . Prin...

Learn Microsoft Stack: Start the C# Beginner Course

Welcome to Learn Microsoft Stack, an independent learning blog maintained by Syed Jawad Hussain Shah. Start with the published C# console lessons and practise each example as you read. Choose a C# learning module C# Beginner Course: Module 1 – Your First Code C# Literals and Variables: Module 2 Guide C# Student Grades Project: Module 3 Guide C# String Formatting: Module 4 Guide How to use the tutorials Read the module overview and check its prerequisites. Run the small examples in a C# console project. Try each challenge before opening the solution. Compare the actual output with the expected result and investigate any differences. Questions or corrections Include the article URL, code and error message when you contact us about a tutorial . Do not include passwords, access tokens or other private data. For the author background and broader topic focus, visit About Learn Microsoft Stack . This blog is not affiliated with Microsoft. Choose a useful starting point Should I start with Pow...