看陳廣老師c#參考視頻總結(第二篇)

日期:2008-6-7
學習內容:數組,類型轉換,using語句的其他用法
遺留問題類型轉換需要進一步的弄清楚

學習總結:

1.       數組:
長度固定的數組的使用方法:
using System;

public class Arrary

{

  

    public static void Main(string[] args)

    {

        int[] arr=new int[]{1,2,3,4,5};//聲明數組的同時對數組進行初始化

        int[] arr1 ={ 1, 2, 3, 4, 5 };//聲明數組並初始化的簡寫方法

        int[] arr2 = new int[5];

        for (int i = 0; i < arr2.Length; i++)

        {

            Console.WriteLine("arr2[{0}]={1}", i, arr[i]);  //格式化輸出

        }

        foreach (int i in arr1)//通過foreach循環遍歷數組

        {

            Console.WriteLine(i);

        }

        for (int i = 0; i < arr.Length; i++)//通過下標索引輸出數組的值

        {

            Console.WriteLine(arr[i]);

        }

    }

}

數組的長度不固定,需要預先輸入的數組的使用方法

注意:同一個域中的靜態成員只能調用本域中的靜態成員

using System;

public class Arrary

{

    public static void PrintArr(int ArrLength) //必須將次方法指定爲靜態的

    {

        int[] arr=new int[ArrLength];

        for(int i=0;i<arr.Length;i++)//給數組元素賦值

        { 

            arr[i]=i;

        }

        Console.WriteLine("打印出數組的值:");

        for(int i=0;i<arr.Length;i++)//打印出數組元素的值

        {

        Console.WriteLine("arr[{0}]={1}",i,arr[i]);

        }

 

    }

    public static void Main(string[] args)

    {

        int i=1;

        while(i>0)

        {

            Console.WriteLine("請輸入數組的長度:");

            i=Int32.Parse(Console.ReadLine());//因爲Readline()方法返回的是字符,需要轉換成整型然後賦值給i

            PrintArr(i);//調用PrintArr方法

        }

    }

}

如果不想把PrintArr聲明爲靜態方法,可以把他放在另外一個類中

注意:如果在此段代碼中再把PrintArr方法加上static關鍵字則會報錯

using System;

public class SetArr

{

    public void PrintArr(int ArrLength)

    {

        int[] arr = new int[ArrLength];

        for (int i = 0; i < arr.Length; i++)//給數組元素賦值

        {

            arr[i] = i;

        }

        Console.WriteLine("打印出數組的值:");

        for (int i = 0; i < arr.Length; i++)//打印出數組元素的值

        {

            Console.WriteLine("arr[{0}]={1}", i, arr[i]);

        }

 

    }

}

public class Arrary

{

  

    public static void Main(string[] args)

    {

        SetArr arr = new SetArr();

        int i=1;

        while(i>0)

        {

            Console.WriteLine("請輸入數組的長度:");

            i=Int32.Parse(Console.ReadLine());//因爲Readline()方法返回的是字符,需要轉換成整型然後賦值給i

            arr.PrintArr(i);//調用PrintArr方法

        }

    }

}

動態數組使用時要用到ArrayList類:

注意:Array類可以定義多維數組,而ArrayList類能定義多爲數組;Array類的數組長度固定,ArrayList類的長度是可以改變的;在ArrayList類中能對元素進行添加,刪除,修改,排序等操作

using System;

using System.Collections;//ArrayList類包含在此命名空間下

public class Arrary

{

    public static void Main(string[] args)

    {

        ArrayList arr = new ArrayList();//聲明ArrayList類的實例

        string str;

        while (true)

        {

            Console.WriteLine("請添加一個字符串到ArrayList中:");

            str=Console.ReadLine();

            if(str=="end")

                break;

            arr.Add(str);//調用Add方法將字符串添加到arr

            Console.WriteLine();

            for(int i=0;i<arr.Count;i++)//輸出arr中的元素

            {

                Console.Write(arr[i]);

                Console.Write(" ");

            }

            Console.Write("\n");

        }

    }

}

二維數組及多維數組的定義及使用

int[,] arr=new int[,]{{1,2,3},{4,5,6}}
int[,] arr={{1,2,3},{4,5,6}}
int[,] arr=new int[2][3]
數組的數組:
int[,] arr=new int[2][]  指定義了數組的行或列的維度
using System;

using System.Collections.Generic;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

 

        public static void Main(string[] args)

        {

            int[,] arr = new int[2,3];

            for (int i = 0; i < 2; i++)  //給數組元素賦值

            {

                for (int j = 0; j < 3; j++)

                {

                    arr[i, j] = i + j;

                }

            }

            for (int i = 0; i < 2; i++)//輸出數組元素

            {

                for (int j = 0; j < 3; j++)

                {

                    Console.Write(arr[i, j]);

                   

                }

                Console.WriteLine();

              }

        }

    }

}

2.類型轉換

類型轉換可以分爲:隱式轉換和顯示轉換
隱式轉換自動執行,轉換規則精度提高,容量增大:byte->short->int->long->float->double;顯示轉換有三種方法:第一種方法直接使用(類型名);第二種方法通過Convert類;第三種方法通過類型自身的Parse方法
值類型之間的轉換:直接在要轉換的數據前面加上要轉換的數據類型
值類型和引用類型之間的轉換:裝箱和拆箱(儘量避免裝箱和拆箱操作,否則會影響系統的性能)
Int類型的數據的原型是:system.int32  long類型的數據的原型是:stystem.int64
求各種數據類型的最大值的方法:數據類型.MAXVALUE
Checkedunchecked操作符只能作用於一條語句checked( ),當時checkeduncheck語句塊能作用於多條語句checked{ }
引用類型間的轉換:CLR允許將對象強制轉換爲其他類型或基類型
Is操作符返回的是bool類型的truefalse
As操作符返回的是是否和給定的類型兼容,兼容不返回空,不兼容返回空值null
注意:as運算符必須和引用類型一起使用
小方法:判斷一個數據是何種類型的方法:
Type t=a.GetType();

Console.WriteLine(t.FullName);
使用is操作符和as操作符的好處是應用程序不會引發異常
3Using語句的其他用法
Using語句除了引用命名空間外,還有一中方法是爲了使unmanged資源儘快釋放,防止內存泄漏。當然您可以調用dispose方法,但有時會忘記。C#提供了另外一種機制,用using語句
確保永遠調用dispose方法,常用於數據庫操作
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Data.SqlClient;

namespace WindowsApplication1

{

    public partial class Form1 : Form

    {

        string str = @"C:\Documents and Settings\Administrator\桌面\1.txt";

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

 

        }

        private void button1_Click(object sender, EventArgs e)

        {

            using(StreamWriter sw=new StreamWriter(str))

            {

                sw.WriteLine("北京理工大學");

                sw.WriteLine("計算機科學與技術系");

            }

            using (StreamReader sr = new StreamReader(str))

            {

                this.textBox1.Text = sr.ReadToEnd();

            }

        }

        //using(SqlConnection conn=new SqlConnection())

        //{

        //    連接數據庫的代碼

        //}

    }

}

遺留問題:類型轉換需要進一步的弄清楚,需要鞏固的知識點

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章