مثال : یک پروژه ی جدید Console سی شارپ را باز کنید و نام آنرا در ابتدا ex06 بگذارید. در این مثال می خواهیم نحوه ی کار با آرایه ها را مرور کنیم :
using System;
namespace ex06
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string[] sGoalList = new string[3];
string sReplyStatement = "You have choosen Goal "";
// Store goals in the array
sGoalList[0] = "Hike the Appalachian Trail";
sGoalList[1] = "Run the marathon";
sGoalList[2] = "Give $1 million to worthwhile causes";
// Store response to goals in the array
//(declaring and initializing on same line)
string[] sGoalResponse = {
"If you are staring from GA, you should get "
+ "started in early spring, so you will "+
"not get caught in snow.",
"Make sure that you have a good pair of shoes.",
"Start saving as soon as possible."};
// Give the user a list of goals to choose from
Console.WriteLine("GOAL LIST");
for(int i = 0; i < sGoalList.Length; i++)
{
Console.WriteLine("Goal " + i +
" - " + sGoalList[i]);
}
// Request the user to choose a goal.
Console.WriteLine (""); // Write an empty line for space
Console.Write("Please choose the number of the "
+ "goal that you want to achieve [0,1,2]: ");
Console.ReadLine();
}
}
}
نکاتی در مورد کد فوق :
1- نحوه ی استفاده از عملگر + را برای اتصال رشته های بلند در کد فوق می توان دید.
2- در سی شارپ پایان خط سمی کولون می باشد. بنابراین نگرانی در مورد چند خطی شدن یک دستور وجود ندارد.
3- هنگامی که آرایه ای را با مقادیر درون آکولادها ، مقدار دهی اولیه می کنید لزومی ندارد طول آن آرایه را مشخص کنید ؛ مانند آرایه sGoalResponse در بالا. در غیر اینصورت حتما باید طول یک آرایه را که معرف تعداد خانه های خالی آن است ، معرفی کنید مانند آرایه sGoalList .
4- فعلا حلقه ی for را در این مثال بخاطر داشته باشید تا در مقاله ی بعدی راجع به آن صحبت کنیم
مینا امیری
سمانه تقدسی پور
زهرا نجفی
مقطع تحصیلی:
کاردانی نرم افزار
<**ادامه مطلب...**>