Cross_Column

Wednesday, 1 July 2026

Comments in C# and Variables



Comments in C# - Beginner Tutorial

Comments in C#

Learn how to write comments in C#, why they are important, and the different types of comments with practical examples.

Table of Contents

  1. What are Comments?
  2. Why Use Comments?
  3. Single-line Comments
  4. Multi-line Comments
  5. XML Documentation Comments
  6. Best Practices
  7. Interview Questions
  8. Practice Exercise

What are Comments?

Comments are notes written inside your source code to explain what the code does. The C# compiler ignores comments, so they do not affect program execution. They make your code easier to understand, maintain, and debug.

Note: Good comments explain why the code exists rather than repeating what the code already says.

Why Use Comments?

  • Improve code readability.
  • Help team members understand the code.
  • Document important logic.
  • Temporarily disable code during debugging.

1. Single-line Comments

Use // to comment a single line.

using System;

class Program
{
    static void Main()
    {
        // Print a welcome message
        Console.WriteLine("Welcome to C#");
    }
}

2. Multi-line Comments

Use /* */ for comments that span multiple lines.

/*
 This program demonstrates
 multi-line comments in C#.
*/
Console.WriteLine("Hello");

3. XML Documentation Comments

Use /// to create XML documentation for methods, classes, and properties.

/// <summary>
/// Adds two numbers.
/// </summary>
int Add(int a, int b)
{
    return a + b;
}
Pro Tip: Visual Studio can automatically generate XML documentation comments by typing /// above a method.

Best Practices

DoDon't
Write meaningful comments.Comment every obvious line.
Keep comments updated.Leave outdated comments.
Use XML comments for public APIs.Use comments instead of clear variable names.
Common Mistake: Beginners often use comments to explain simple statements. Instead, write clean, descriptive code and use comments only where additional context is helpful.

Summary

  • // → Single-line comment
  • /* ... */ → Multi-line comment
  • /// → XML documentation comment

Interview Questions

  1. What are comments in C#?
  2. Does the compiler execute comments?
  3. What is the difference between // and /* */?
  4. What are XML documentation comments used for?

Practice Exercise

  • Create a program with one single-line comment.
  • Add a multi-line comment describing the program.
  • Create a method and document it with XML comments.

What's Next?

In the next tutorial, you'll learn Variables in C# and how to store and manipulate data.

Variables in C# - Beginner Tutorial

Variables in C#

Understand variables, declaration, initialization, naming rules and examples.

Table of Contents

  1. What is a Variable?
  2. Syntax
  3. Examples
  4. Naming Rules
  5. Best Practices
  6. FAQ

What is a Variable?

A variable is a named memory location used to store data. The value of a variable can change while the program is running.

Note: Before using a variable, you should declare it with an appropriate data type.

Variable Syntax

dataType variableName = value;

Example:

int age = 25;
string name = "Chandan";
double salary = 45000.50;
char grade = 'A';
bool isActive = true;

Complete Example

using System;

class Program
{
    static void Main()
    {
        string name = "John";
        int age = 30;

        Console.WriteLine("Name: " + name);
        Console.WriteLine("Age: " + age);
    }
}
Tip: Choose meaningful variable names such as employeeName instead of x.

Variable Naming Rules

RuleExample
Must start with a letter or underscore_count, total
Cannot start with a number❌ 1value
Case-sensitiveage and Age are different
No spacesemployeeName ✔

Types of Variable Declaration

  • Declaration: int marks;
  • Initialization: marks = 90;
  • Declaration + Initialization: int marks = 90;
Common Mistake: Using a variable before assigning a value can result in compilation errors for local variables.

Best Practices

  • Use descriptive names.
  • Follow camelCase for local variables.
  • Initialize variables whenever possible.
  • Avoid one-letter names except in loops.

Summary

ConceptDescription
VariableStores data in memory.
DeclarationDefines name and data type.
InitializationAssigns the first value.

Interview Questions

  1. What is a variable?
  2. What is variable initialization?
  3. What are the naming rules?
  4. Why are data types required?

Practice Exercise

  • Create variables for your name, age and city.
  • Print all values using Console.WriteLine().
  • Update the age and print it again.

FAQ

Can a variable change its value?
Yes. That's why it is called a variable.

Can two variables have the same name?
Not within the same scope.


Next Tutorial: Data Types in C#

No comments:

Post a Comment

Few More

Recursion in C#

Recursion in C# | Beginner Tutorial Recursion in C# Learn how recursion works in C#, understand the base case and re...