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
- What are Comments?
- Why Use Comments?
- Single-line Comments
- Multi-line Comments
- XML Documentation Comments
- Best Practices
- Interview Questions
- 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.
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;
}
/// above a method.
Best Practices
| Do | Don'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. |
Summary
//→ Single-line comment/* ... */→ Multi-line comment///→ XML documentation comment
Interview Questions
- What are comments in C#?
- Does the compiler execute comments?
- What is the difference between
//and/* */? - 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.
Variables in C#
Understand variables, declaration, initialization, naming rules and examples.
Table of Contents
- What is a Variable?
- Syntax
- Examples
- Naming Rules
- Best Practices
- 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.
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);
}
}
employeeName instead of x.Variable Naming Rules
| Rule | Example |
|---|---|
| Must start with a letter or underscore | _count, total |
| Cannot start with a number | ❌ 1value |
| Case-sensitive | age and Age are different |
| No spaces | employeeName ✔ |
Types of Variable Declaration
- Declaration:
int marks; - Initialization:
marks = 90; - Declaration + Initialization:
int marks = 90;
Best Practices
- Use descriptive names.
- Follow camelCase for local variables.
- Initialize variables whenever possible.
- Avoid one-letter names except in loops.
Summary
| Concept | Description |
|---|---|
| Variable | Stores data in memory. |
| Declaration | Defines name and data type. |
| Initialization | Assigns the first value. |
Interview Questions
- What is a variable?
- What is variable initialization?
- What are the naming rules?
- 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