当前位置:网站首页>C array
C array
2022-04-23 06:24:00 【Wood acridine】
C# Array
Characteristics of array :
The values stored in an array are of the same data type .
A specified element in an array is accessed through an index .
All arrays consist of contiguous memory locations .
Arrays are indexed from zero , That is, the array index starts from zero .
One dimensional array
grammar :
Define an array : data type [ ] Array name ;
initialization Elements in an array :
- data type [ ] Array name =new data type [ length ];
example :int[] intArr = new int[5]; The length of the array 5 The content of each element is not specified , Not initialized
- data type [ ] Array name ={ value 1, value 2,……}
example :int[] intArr1 = { 1, 2, 3, 4 }; Initializing arrays directly , The length of the array is calculated by the compiler . And initialize
- data type [ ] Array name =new data type [ length ] { value 1, value 2,……}
example :int[] intArr2 = new int[3] { 5, 6,7}; Indicates the length of the array 3, And initialize
Two dimensional array :
grammar :
data type [ ,] Array name ;
Create a two-dimensional array and initialize it :
data type [ ,] Array name =new data type [ m,n]{ { ,},{ ,}};
example :int[,] intDArr = new int[2, 2] { { 1, 2 }, { 3, 4 } };
foreach Traverse
grammar :foreach( data type Variable name in Array name ){ Sentence block ;}
Traversing a one-dimensional array
example :int[] intArr1 = { 1, 2, 3, 4 };
foreach (int item in intArr1)
{
Console.WriteLine(item);
}
Traversing a two-dimensional array
example : int[,] intDArr = new int[2, 2] { { 1, 2 }, { 3, 4 } };
for (int i = 0; i < intDArr.GetLength(0); i++)
{
for (int j = 0; j < intDArr.GetLength(1); j++)
{
Console.Write("{0}\t", intDArr[i, j]);
}
Console.Write("\n"); Line break
}
notes :foreach Can only be used for arrays 、 String or collection data type
Type of structure
Structure type is a custom value type .
Definition grammar :
Access modifier struct Structure name { Structural members }
Structures and classes difference :
Structure :
It is allowed not to use new Exemplify it
There is no default constructor
Can't inherit class
Not allowed abstract、protected as well as sealed modification
class :
You have to use new Instantiation
There is a default construction method
Can inherit classes
Allow to use abstract、protected as well as sealed modification
Enumeration type
Enumeration type is a special value type
grammar :
Access modifier enum Variable name : data type { value 1, value 2,}
Enumeration class Type determines the maximum content that can be contained in an enumerated class .
data type : It refers to the data type of the value in the enumeration . Can only be integer type . Such as byte、short、int、long etc. .
example : enum Week : short
{
SUN, // Sunday
MON, // Monday
TUE, // Tuesday
WED, // Wednesday
THU, // Thursday
FRI, // Friday
SAT, // Saturday
UNDEFIN,
}
static void PrintWeek(Week week)
{
switch (week)
{
case Week.MON:// Monday
Console.WriteLine(" Monday ");
break;
case Week.TUE:// Tuesday
Console.WriteLine(" Tuesday ");
break;
case Week.WED:// Wednesday
Console.WriteLine(" Wednesday ");
break;
case Week.THU:// Thursday
Console.WriteLine(" Thursday ");
break;
case Week.FRI:// Friday
Console.WriteLine(" Friday ");
break;
case Week.SAT:// Saturday
Console.WriteLine(" Saturday ");
break;
case Week.SUN:// Sunday
Console.WriteLine(" Sunday ");
break;
}
}
版权声明
本文为[Wood acridine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210616405278.html
边栏推荐
- Failure to deliver XID in Seata distributed transaction project
- Example of reentrant lock thread waiting to wake up
- Use of multithreaded executors
- Rainbow (DP)
- 编程记录——图片旋转函数scipy.ndimage.rotate()的简单使用和效果观察
- Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
- container
- St table template
- Plane semi intersecting plate
- C3p0 database connection pool usage
猜你喜欢

Algèbre linéaire chapitre 2 - matrice et son fonctionnement

Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()

自动控制(韩敏版)

SQL injection

Preparedstatement prevents SQL injection

Class loading and classloader understanding

Kalman filter and inertial integrated navigation

检测技术与原理

编程记录——图片旋转函数scipy.ndimage.rotate()的简单使用和效果观察
![图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi](/img/1b/4eea05e2634780f45b44273d2764e3.png)
图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi
随机推荐
Best practices for MySQL storage time
[leetcode 290] word rules
Problems and solutions of database migration
Pytorch learning record (III): structure of neural network + using sequential and module to define the model
Installation and usage skills of idea
PyTorch入门小笔记——利用简单例子观察前向传播各个层输出的size
Fact final variable and final variable
A sharp tool to improve work efficiency
A general U-shaped transformer for image restoration
Numpy common function table sorting of data processing
10.Advance Next Round
Algèbre linéaire chapitre 2 - matrice et son fonctionnement
Formation à la programmation
Pytorch introduction notes - use a simple example to observe the output size of each layer of forward propagation
Use Matplotlib. In Jupiter notebook Pyplot server hangs up and crashes
[leetcode 459] duplicate substring
Linear algebra Chapter 2 - matrices and their operations
Contrôle automatique (version Han min)
检测技术与原理
[transfer] MySQL: how many rows of data can InnoDB store in a B + tree?