一、數(shù)組
聲明一個(gè)數(shù)組:datatype[] arrayName;
聲明一個(gè)數(shù)組是并不會(huì)在內(nèi)存中對數(shù)組進(jìn)行初始化昔汉,需要用new關(guān)鍵字來創(chuàng)建數(shù)組的實(shí)例宪迟。
例如:int data =new int[10];
對于數(shù)組的賦值有多種方法:
- 對單獨(dú)的數(shù)組元素進(jìn)行賦值:int[0] = 1;
- 在聲明數(shù)組的同時(shí)進(jìn)行賦值:int[] data = { 10, 20, 30, 40, 50};
- 創(chuàng)建并初始化一個(gè)數(shù)組:int[] data = new int[] {10, 20, 30};
- 從一個(gè)數(shù)組賦值到另一個(gè)數(shù)組:
int[] data1 = new int[] {10, 20};
int[] data2 = data1;
可以用foreach來訪問數(shù)組元素
using System;
using System.IO;
namespace WriteApplication
{
class WriteTest
{
//主函數(shù)
static void Main(string[] args)
{
int[] n = new int[] {10, 20, 30};
int i = 0;
foreach(int j in n)
{
Console.WriteLine("Element[{0}] = {1}", i, j);
i++;
}
Console.ReadKey();
}
}
}
代碼運(yùn)行輸出:
Element[0] = 10
Element[1] = 20
Element[2] = 30
二、字符串
- 字符串定義:string s = "hi";
- 字符串與字符串連接:string s = s1 + s2;
- string.Length:獲取string對象中的字符數(shù)
- public static int Compare( string strA, string strB):比較兩個(gè)指定的string是否相同望忆,相同返回0吴叶,區(qū)分大小寫
- public static string Concat( string str0, string str1, … ):連接string對象
- public bool Contains( string value):一個(gè)string是否存在于另一個(gè)string中
- public static string Join( string separator, string[] value):連接一個(gè)字符串?dāng)?shù)組中的所有元素阐虚,并使用分隔符separator分割
using System;
namespace WriteApplication
{
class WriteTest
{
//主函數(shù)
static void Main(string[] args)
{
//字符串連接
string s1, s2;
s1 = "hi";
s2 = "hello";
string s3 = s1 +s2;
Console.WriteLine(s3);
//將char中字符連接轉(zhuǎn)為string
char[] letters = { 'h', 'e', 'l', 'l', 'o'};
string greetings = new string(letters);
Console.WriteLine(greetings);
//將字符串組連接
string[] sarray = { "hi", "hello", "bye"};
string message = String.Join(" ", sarray);
Console.WriteLine(message);
Console.ReadKey();
}
}
}
代碼運(yùn)行輸出:
hihello
hello
hi hello bye
比較字符串
using System;
namespace WriteApplication
{
class WriteTest
{
//主函數(shù)
static void Main(string[] args)
{
string s1, s2;
s1 = "helLo";
s2 = "hello";
int i1 = String.Compare(s1, s2);
Console.WriteLine(i1);
string s3, s4;
s3 = "hello";
s4 = "hello";
int i2 = String.Compare(s3, s4);
Console.WriteLine(i2);
Console.ReadKey();
}
}
}
代碼運(yùn)行結(jié)果:
1
0
字符串包含關(guān)系
using System;
namespace WriteApplication
{
class WriteTest
{
//主函數(shù)
static void Main(string[] args)
{
string s1, s2;
s1 = "hello";
s2 = "wahellogood";
if( s2.Contains(s1) )
Console.WriteLine("hello");
Console.ReadKey();
}
}
}
代碼運(yùn)行結(jié)果:
hello
三、結(jié)構(gòu)體
定義結(jié)構(gòu)體:
struct Books
{
public string title;
public string author;
public string subject;
public int book_id;
};
聲明結(jié)構(gòu)體:Books Book1;
結(jié)構(gòu)體內(nèi)容賦值:Book1.title = "hello";
using System;
using System.Text;
struct Books
{
private string title;
private string author;
private string subject;
private int book_id;
public void getValues(string t, string a, string s, int id)
{
title = t;
author = a;
subject = s;
book_id =id;
}
public void display()
{
Console.WriteLine("Title : {0}", title);
Console.WriteLine("Author : {0}", author);
Console.WriteLine("Subject : {0}", subject);
Console.WriteLine("Book_id :{0}", book_id);
}
};
public class testStructure
{
public static void Main(string[] args)
{
Books Book1 = new Books(); /* 聲明 Book1蚌卤,類型為 Book */
Books Book2 = new Books(); /* 聲明 Book2实束,類型為 Book */
/* book 1 詳述 */
Book1.getValues("C Programming",
"Nuha Ali", "C Programming Tutorial",6495407);
/* book 2 詳述 */
Book2.getValues("Telecom Billing",
"Zara Ali", "Telecom Billing Tutorial", 6495700);
/* 打印 Book1 信息 */
Book1.display();
/* 打印 Book2 信息 */
Book2.display();
Console.ReadKey();
}
}
代碼運(yùn)行結(jié)果:
Title : C Programming
Author : Nuha Ali
Subject : C Programming Tutorial
Book_id :6495407
Title : Telecom Billing
Author : Zara Ali
Subject : Telecom Billing Tutorial
Book_id :6495700
- 結(jié)構(gòu)體可帶方法,字段逊彭,索引咸灿,屬性,運(yùn)算符方法和事件诫龙,可以定義構(gòu)造函數(shù)析显,但不能定義析構(gòu)函數(shù)。
- 結(jié)構(gòu)體在聲明的時(shí)候不能進(jìn)行賦值签赃,而類可以谷异。
- 結(jié)構(gòu)體不能繼承,成員不能指定為abstract锦聊,virtual歹嘹,protected。
- 可以使用New操作符創(chuàng)建一個(gè)結(jié)構(gòu)對象孔庭,如果不使用New操作符尺上,則需要所有字段初始化后才能進(jìn)行賦值及使用。
四圆到、枚舉
聲明枚舉的方法:
enum <enum_name>
{
enumeration list;
};
其中怎抛,enum_name是枚舉名稱焊刹,enumeration list是用逗號分隔的標(biāo)識符列表长搀,如:enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
枚舉列表中的每個(gè)符號代表一個(gè)整數(shù)值,每個(gè)比前一個(gè)大1束凑,枚舉列表中的第一個(gè)符號為0挣菲。
using System;
public class EnumTest
{
enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
static void Main()
{
int x = (int)Day.Sun;
int y = (int)Day.Fri;
Console.WriteLine("Sun = {0}", x);
Console.WriteLine("Fri = {0}", y);
Console.ReadKey();
}
}
代碼運(yùn)行結(jié)果:
Sun = 0
Fri = 5