Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
Learn C# Programming
Next article icon

C# Comments

Last Updated : 11 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Comments are an essential part of the code. They allow developers to explain their code, document important information, and make the code easier to understand. Whatever you write in comments Compilers ignore it and do not execute them.

In C#, There are three types of comments which are defined below

  1. Single-line comments
  2. Multi-line Comments
  3. XML comments

Comments are non-executable code lines ignored by the compiler. They offer explanations and context for developers, enhancing readability in complex projects.

Note: For writing good and productive comments please remember the points mentioned below:

  • Comments are self-explanatory. Comments should add value by explaining complex or non-obvious logic.
  • Comments should be clear and concise. Avoid writing overly complex comments that could confuse the reader.

1. Single-Line comments

A single-line comment in C# is used to add a brief explanation or note about a specific line of code. This type of comment starts with two forward slashes // and continues until the end of the line.

Syntax:

// This is a single-line comment which used to explain the code

Example:

C#
// Using single-line comment using System;  public class Geeks  {     public static void Main()     {         // This is a single-line comment explaining the next         // line of code         // This is not going to interrupt the flow of the         // program.          int number = 300;         // Assigning 10 to the variable number         Console.WriteLine(number);         // Printing the value of number     } } 

Output
300 

2. Multi-Line Comments

A multi-line comment is used to comment out a block of code that spans multiple lines. begins with /* and ends with */. Everything between these symbols is considered part of the comment. And not executed by the compiler.

Syntax:

/*
This is a multi-line comment.

int number = 20;
Console.WriteLine(“this is alternative code”);

It can span multiple lines.
Each line within the block is part of the comment.

*/

Example:

C#
// Using multi-line comments using System;  public class Geeks  {   	/*         This block of code does the following:         - Initializes an integer variable.         - Prints the value of the variable.     */        public static void Main()     {             int number = 150;         Console.WriteLine(number);     } } 

Output
20 

3. XML Comments

It is a special type of comment in C# and is used to create the documentation of C# code by adding XML elements in the source code. XML elements are added in XML Documentation Comments of C#. They are written using XML tags and begin with ///

Note: These comments are used to describe the purpose and functionality of methods, classes, properties, and other members of your code.

Syntax:

/// <summary>
/// This method adds two numbers and returns the result.
/// </summary>
/// <param name=”a”>The first number to add.</param>
/// <param name=”b”>The second number to add.</param>
/// <returns>The sum of the two numbers.</returns>

public int Add(int a, int b)
{
return a + b;
}

Example:

C#
// Using xml comments using System;  public class Geeks  {     /// <summary>     /// Adds two numbers and returns the result     /// </summary>     /// <param name="x">The first number to add</param>     /// <param name="y">The second number to add</param>     /// <returns>The sum of x and y</returns>        	public int Add(int x, int y) {         return x + y;      }      public static void Main()     {         Geeks calc = new Geeks();         Console.WriteLine(calc.Add(8, 8));     } } 

Output
16 




Next Article
Learn C# Programming

M

Mithun Kumar
Improve
Article Tags :
  • C#
  • CSharp-Basics

Similar Reads

  • Learn C# Programming
    This C# Learning Guide is perfect for both beginners and experienced programmers. This specially designed, free online guide will help you learn C# programming efficiently from scratch, covering all topics from basics to advanced, including web development, the .NET framework, and more, with real-wo
    15+ min read
  • C# Interview Questions and Answers
    C# is the most popular general-purpose programming language and was developed by Microsoft in 2000, renowned for its robustness, flexibility, and extensive application range. It is simple and has an object-oriented programming concept that can be used for creating different types of applications. He
    15+ min read
  • Hello World Program : First program while learning Programming
    In this article, I'll show you how to create your first Hello World computer program in various languages. Along with the program, comments are provided to help you better understand the terms and keywords used in theLearning program. Programming can be simplified as follows: Write the program in a
    6 min read
  • C Functions
    A function in C is a set of statements that when called perform some specific tasks. It is the basic building block of a C program that provides modularity and code reusability. The programming statements of a function are enclosed within { } braces, having certain meanings and performing certain op
    10 min read
  • Comments in Octave GNU
    Octave is open-source, free available for many of the platforms. It is a high-level language. It comes up with a text interface along with an experimental graphical interface. It is also used for various Machine Learning algorithms for solving various numeric problems. You can say that it is similar
    2 min read
  • C Programs
    To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and
    8 min read
  • Misc C Programs
    C Program to print environment variablesC Program to Swap two NumbersC program swap two numbers without using a temporary variableC Program to check if a given year is leap yearC Program to sum the digits of a given number in single statement?C program to print numbers from 1 to 100 without using lo
    1 min read
  • C Multiple Choice Questions
    C is the most popular programming language developed by Dennis Ritchie at the Bell Laboratories in 1972 to develop the UNIX operating systems. It is a general-purpose and procedural programming language. It is faster than the languages like Java and Python. C is very versatile it can be used in both
    4 min read
  • String C/C++ Programs
    C program to swap two StringsC Program to Sort an array of names or stringsC Program to Check if a Given String is PalindromeC/C++ Program for Return maximum occurring character in the input stringC/C++ Program for Remove all duplicates from the input string.C/C++ Program for Print all the duplicate
    3 min read
  • C Library Functions
    The Standard Function Library in C is a huge library of sub-libraries, each of which contains the code for several functions. In order to make use of these libraries, link each library in the broader library through the use of header files. The actual definitions of these functions are stored in sep
    10 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences