
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Why do we use comma operator in C#?
Comma operator in C# can be used as a separator in method argument list. You can also use it is an operator in a for statement.
The following is an example showing using a comma operator in a for statement for initialization −
for (int i = begin, j = 1; i <= end; i++, j++)
Use it in Console.WriteLine for displaying the values as well −
Console.Write("{0} : {1} ", i, (char)i);
Here is the complete code −
Example
using System; class Demo { const int begin = 45; const int end = 60; const int line = 1; static public void Main() { for (int i = begin, j = 1; i <= end; i++, j++) { Console.Write("{0} : {1} ", i, (char)i); if (0 == (j % line)) { Console.WriteLine(""); } } } }
Output
45 : - 46 : . 47 : / 48 : 0 49 : 1 50 : 2 51 : 3 52 : 4 53 : 5 54 : 6 55 : 7 56 : 8 57 : 9 58 : : 59 : ; 60 : <
- Related Articles
- Why do we use "use strict" in JavaScript?
- Where do we use scope Resolution Operator (::) in C#?
- Why do we use random.seed() in Python?
- Why do we use interfaces in Java?
- Why do we use pandas in python?
- Why do we use brackets in BODMAS?
- Why do we Use JavaScript in HTML?
- Why do we use Convex Mirrors ?
- How do we use equivalence (“equality”) operator in Python classes?
- Why do we use JSON.stringify() method in jQuery?
- Why do we use DOCTYPES in HTML document?
- Why do we use modifiers in C/C++?
- Why do we use restrict qualifier in C++?
- Why do we use const qualifier in C++?
- Why do we use internal keyword in C#?

Advertisements