C#学习 D3

一、数组

声明一个数组:datatype[] arrayName;
声明一个数组是并不会在内存中对数组进行初始化,需要用new关键字来创建数组的实例。
例如:int data =new int[10];
对于数组的赋值有多种方法:

  • 对单独的数组元素进行赋值:int[0] = 1;
  • 在声明数组的同时进行赋值:int[] data = { 10, 20, 30, 40, 50};
  • 创建并初始化一个数组:int[] data = new int[] {10, 20, 30};
  • 从一个数组赋值到另一个数组:
    int[] data1 = new int[] {10, 20};
    int[] data2 = data1;
    可以用foreach来访问数组元素
using System;
using System.IO;

namespace WriteApplication
{
    class WriteTest
    {
        //主函数
        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();
        }
    }
}

代码运行输出:
Element[0] = 10
Element[1] = 20
Element[2] = 30


二、字符串

  • 字符串定义:string s = "hi";
  • 字符串与字符串连接:string s = s1 + s2;
  • string.Length:获取string对象中的字符数
  • public static int Compare( string strA, string strB):比较两个指定的string是否相同,相同返回0,区分大小写
  • public static string Concat( string str0, string str1, … ):连接string对象
  • public bool Contains( string value):一个string是否存在于另一个string中
  • public static string Join( string separator, string[] value):连接一个字符串数组中的所有元素,并使用分隔符separator分割
using System;

namespace WriteApplication
{
    class WriteTest
    {
        //主函数
        static void Main(string[] args)
        {
            //字符串连接
            string s1, s2;
            s1 = "hi";
            s2 = "hello";
            string s3 = s1 +s2;
            Console.WriteLine(s3);

            //将char中字符连接转为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();
        }
    }
}

代码运行输出:
hihello
hello
hi hello bye

比较字符串

using System;

namespace WriteApplication
{
    class WriteTest
    {
        //主函数
        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();
        }
    }
}

代码运行结果:
1
0

字符串包含关系

using System;

namespace WriteApplication
{
    class WriteTest
    {
        //主函数
        static void Main(string[] args)
        {
            string s1, s2;
            s1 = "hello";
            s2 = "wahellogood";
            if( s2.Contains(s1) )
                Console.WriteLine("hello");
            Console.ReadKey();
        }
    }
}

代码运行结果:
hello


三、结构体

定义结构体:
struct Books
{
public string title;
public string author;
public string subject;
public int book_id;
};
声明结构体:Books Book1;
结构体内容赋值: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();

   }
}

代码运行结果:
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

  • 结构体可带方法,字段,索引,属性,运算符方法和事件,可以定义构造函数,但不能定义析构函数。
  • 结构体在声明的时候不能进行赋值,而类可以。
  • 结构体不能继承,成员不能指定为abstract,virtual,protected。
  • 可以使用New操作符创建一个结构对象,如果不使用New操作符,则需要所有字段初始化后才能进行赋值及使用。

四、枚举

声明枚举的方法:
enum
{
enumeration list;
};
其中,enum_name是枚举名称,enumeration list是用逗号分隔的标识符列表,如:enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
枚举列表中的每个符号代表一个整数值,每个比前一个大1,枚举列表中的第一个符号为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();
    }
}

代码运行结果:
Sun = 0
Fri = 5

你可能感兴趣的:(C#学习 D3)