C# Arrays - W3Schools
文章推薦指數: 80 %
Create an Array ... Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, ...
Tutorials
References
Exercises
Videos
Menu
Login
GetCertified
WebsiteNEW
ProNEW
HTML
CSS
JAVASCRIPT
SQL
PYTHON
PHP
BOOTSTRAP
HOWTO
W3.CSS
JAVA
JQUERY
C
C++
C#
R
React
Darkmode
Darkcode
×
Tutorials
HTMLandCSS
LearnHTML
LearnCSS
LearnRWD
LearnBootstrap
LearnW3.CSS
LearnColors
LearnIcons
LearnGraphics
LearnSVG
LearnCanvas
LearnHowTo
LearnSass
DataAnalytics
LearnAI
LearnMachineLearning
LearnDataScience
LearnNumPy
LearnPandas
LearnSciPy
LearnMatplotlib
LearnStatistics
LearnExcel
XMLTutorials
LearnXML
LearnXMLAJAX
LearnXMLDOM
LearnXMLDTD
LearnXMLSchema
LearnXSLT
LearnXPath
LearnXQuery
JavaScript
LearnJavaScript
LearnjQuery
LearnReact
LearnAngularJS
LearnJSON
LearnAJAX
LearnAppML
LearnW3.JS
Programming
LearnPython
LearnJava
LearnC
LearnC++
LearnC#
LearnR
LearnKotlin
LearnGo
LearnDjango
LearnTypeScript
ServerSide
LearnSQL
LearnMySQL
LearnPHP
LearnASP
LearnNode.js
LearnRaspberryPi
LearnGit
LearnAWSCloud
WebBuilding
CreateaWebsiteNEW
WhereToStart
WebTemplates
WebStatistics
WebCertificates
WebDevelopment
CodeEditor
TestYourTypingSpeed
PlayaCodeGame
CyberSecurity
Accessibility
DataAnalytics
LearnAI
LearnMachineLearning
LearnDataScience
LearnNumPy
LearnPandas
LearnSciPy
LearnMatplotlib
LearnStatistics
LearnExcel
LearnGoogleSheets
XMLTutorials
LearnXML
LearnXMLAJAX
LearnXMLDOM
LearnXMLDTD
LearnXMLSchema
LearnXSLT
LearnXPath
LearnXQuery
×
References
HTML
HTMLTagReference
HTMLBrowserSupport
HTMLEventReference
HTMLColorReference
HTMLAttributeReference
HTMLCanvasReference
HTMLSVGReference
GoogleMapsReference
CSS
CSSReference
CSSBrowserSupport
CSSSelectorReference
Bootstrap3Reference
Bootstrap4Reference
W3.CSSReference
IconReference
SassReference
JavaScript
JavaScriptReference
HTMLDOMReference
jQueryReference
AngularJSReference
AppMLReference
W3.JSReference
Programming
PythonReference
JavaReference
ServerSide
SQLReference
MySQLReference
PHPReference
ASPReference
XML
XMLDOMReference
XMLHttpReference
XSLTReference
XMLSchemaReference
CharacterSets
HTMLCharacterSets
HTMLASCII
HTMLANSI
HTMLWindows-1252
HTMLISO-8859-1
HTMLSymbols
HTMLUTF-8
×
ExercisesandQuizzes
Exercises
HTMLExercises
CSSExercises
JavaScriptExercises
SQLExercises
MySQLExercises
PHPExercises
PythonExercises
NumPyExercises
PandasExercises
SciPyExercises
jQueryExercises
JavaExercises
C++Exercises
C#Exercises
RExercises
KotlinExercises
GoExercises
BootstrapExercises
Bootstrap4Exercises
Bootstrap5Exercises
GitExercises
Quizzes
HTMLQuiz
CSSQuiz
JavaScriptQuiz
SQLQuiz
MySQLQuiz
PHPQuiz
PythonQuiz
NumPyQuiz
PandasQuiz
SciPyQuiz
jQueryQuiz
JavaQuiz
C++Quiz
C#Quiz
RQuiz
KotlinQuiz
XMLQuiz
BootstrapQuiz
Bootstrap4Quiz
Bootstrap5Quiz
CyberSecurityQuiz
AccessibilityQuiz
Courses
HTMLCourse
CSSCourse
JavaScriptCourse
FrontEndCourse
SQLCourse
PHPCourse
PythonCourse
NumPyCourse
PandasCourse
DataAnalyticsCourse
jQueryCourse
JavaCourse
C++Course
C#Course
RCourse
XMLCourse
CyberSecurityCourse
AccessibilityCourse
Certificates
HTMLCertificate
CSSCertificate
JavaScriptCertificate
FrontEndCertificate
SQLCertificate
PHPCertificate
PythonCertificate
DataScienceCertificate
Bootstrap3Certificate
Bootstrap4Certificate
jQueryCertificate
JavaCertificate
C++Certificate
ReactCertificate
XMLCertificate
×
Tutorials
References
Exercises
PaidCourses
Spaces
Videos
Shop
Pro
C#Tutorial
C#HOME
C#Intro
C#GetStarted
C#Syntax
C#Output
C#Comments
C#Variables
C#DataTypes
C#TypeCasting
C#UserInput
C#Operators
C#Math
C#Strings
C#Booleans
C#If...Else
C#Switch
C#WhileLoop
C#ForLoop
C#Break/Continue
C#Arrays
C#Methods
C#Methods
C#MethodParameters
C#MethodOverloading
C#Classes
C#OOP
C#Classes/Objects
C#ClassMembers
C#Constructors
C#AccessModifiers
C#Properties
C#Inheritance
C#Polymorphism
C#Abstraction
C#Interface
C#Enums
C#Files
C#Exceptions
C#HowTo
AddTwoNumbers
C#Examples
C#Examples
C#Compiler
C#Exercises
C#Quiz
C#Certificate
C#Arrays
❮Previous
Next❯
CreateanArray
Arraysareusedtostoremultiplevaluesinasinglevariable,insteadofdeclaringseparatevariablesforeach
value.
Todeclareanarray,definethevariabletypewithsquarebrackets:
string[]cars;
Wehavenowdeclaredavariablethatholdsanarrayofstrings.
Toinsertvaluestoit,wecanuseanarrayliteral-placethevaluesinacomma-separatedlist,insidecurlybraces:
string[]cars={"Volvo","BMW","Ford","Mazda"};
Tocreateanarrayofintegers,youcouldwrite:
int[]myNum={10,20,30,40};
AccesstheElementsofanArray
Youaccessanarrayelementbyreferringtotheindexnumber.
Thisstatementaccessesthevalueofthefirstelementincars:
Example
string[]cars={"Volvo","BMW","Ford","Mazda"};
Console.WriteLine(cars[0]);
//OutputsVolvo
TryitYourself»
Note:Arrayindexesstartwith0:[0]isthefirstelement.[1]isthesecond
element,etc.
ChangeanArrayElement
Tochangethevalueofaspecificelement,refertotheindexnumber:
Example
cars[0]="Opel";
Example
string[]cars={"Volvo","BMW","Ford","Mazda"};
cars[0]="Opel";
Console.WriteLine(cars[0]);
//NowoutputsOpelinsteadofVolvo
TryitYourself»
ArrayLength
Tofindouthowmanyelementsanarrayhas,usethe
Lengthproperty:
Example
string[]cars={"Volvo","BMW","Ford","Mazda"};
Console.WriteLine(cars.Length);
//Outputs4
TryitYourself»
LoopThroughanArray
Youcanloopthroughthearrayelementswiththeforloop,andusethe
Length
propertytospecifyhowmanytimestheloopshouldrun.
Thefollowingexampleoutputsallelementsinthecars
array:
Example
string[]cars={"Volvo","BMW","Ford","Mazda"};
for(inti=0;i
延伸文章資訊
- 1C# Arrays - W3Schools
Create an Array ... Arrays are used to store multiple values in a single variable, instead of dec...
- 2一維陣列- C# 程式設計手冊 - Microsoft Docs
int[] array = new int[5];. 這個陣列包含從 array[0] 到 array[4] 的項目。 陣列的專案會初始化為整數之元素類型的 0 預設值。
- 3C# Array 陣列- 教學筆記(使用visual studio) - 關於網路那些事...
C#提供了許多預設class可以用來儲存資料,其中一個就是array陣列陣列可以 ... 例如: int[,] x= new int[4,3]; 建立一個二維、 4x3的array 等同: in...
- 4C# | Arrays of Strings - GeeksforGeeks
C# | Arrays of Strings ... declares & initializes string array String[] s1 = new String[2]; // as...
- 5C# - Arrays - Tutorialspoint
int [] marks = new int[] { 99, 98, 92, 97, 95}; int[] score = marks;. When you create an array, C...