- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to assign multiple values to same variable in C#?
To set multiple values to same variable, use arrays in C#. Let’s say instead of taking 5 variables, set these 5 variables using arrays in a single variable.
The following is an example to set three values to a single variable with a string array −
string[] arr = new string[3];
Let us now initialize it −
string[] arr = new string[3] {"one", "two", "three"};
The following is the complete example −
Example
using System; public class Demo { static void Main(string[] args) { string[] arr = new string[3] {"one", "two", "three"}; for (int i = 0; i < arr.Length; i++) { Console.WriteLine("Values: " + arr[i]); } Console.ReadKey(); } }
Output
Values: one Values: two Values: three
Advertisements