c#数组赋值语句问题

来源:百度知道 编辑:UC知道 时间:2024/05/21 08:17:46
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] QW = new int[3];
int[] QW ={ 4,5,6 };
}
}
}
首先确定空间为3 再赋值 这样为什么不行 我记得c++好像可以的

问题出在:
int[] QW = new int[3];
int[] QW ={ 4,5,6 };这两句上
第一句,你已经声明了一个整型数组,QW,而第二句,你又声明了这个变量,这样就不对了。
正确形式:
int[] QW = new int[3];
QW ={ 4,5,6 };
或 int[] QW = new int[3]{4,5,6 };

int[] QW = new int[]{4,5,6 };

或者

int[] QW = new int[3];

QW[0] =4;
QW[1] =5;

QW[2] =6;

int[] QW = new int[3]{4,5,6 };