變量,常量,引用類型,值類型,枚舉類型, 值類型和引用類型的簡單區別,類型轉換,裝箱與拆箱


           變量
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace BianLiang
{
    class Program
    {
        static void Main(string[] args)
        {


              //聲明變量
            //int LS;                      //聲明一個整型變量
            //string Str1, Str2, Str3;     //同時聲明3個字符型變量


            //int a = 927;                                 //初始化整型變量a
            //string x = "用一生下載你", y = "雲樺湘楓", z = "一生所愛";      //初始化字符型變量x、y和z


                  //變量的作用域
            ////調用for語句循環輸出數字
            //for (int i = 0; i <= 20; i++)    //for循環內的局部變量
            //{
            //    Console.WriteLine(i.ToString());    //輸出0~20的數字
            //}
            //Console.ReadLine();


               //變量的賦值
            //int sum;           //聲明一個變量
            //sum = 2008;        //使用複製運算符"="給變量賦值






            int sum,num;    //聲明兩個變量
            sum = 927;      //給變量sum賦值爲927
            num = sum;      //將變量sum賦值給變量num


        }
    }
}


                常量
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ChangLiang
{
    class Program
    {
        static void Main(string[] args)
        {
            //const double PI = 3.1415926;            //正確的聲明方法
            //const int MyInt;                        //錯誤:定義常量是沒有初始化


            int MyInt = 927;                   //聲明一個整型變量
            const int MyWInt = 112;            //聲明一個整型常量
            Console.WriteLine("變量 MyInt={0}", MyInt);       //輸出
            Console.WriteLine("常量 MyWInt={0}", MyWInt);     //輸出
            MyInt = 1039;                                     //重新將變量賦值爲1039
            Console.WriteLine("變量 MyInt={0}", MyInt);        //輸出
            Console.ReadLine();






        }
    }
}




                      引用類型


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Yingyongleixing
{
    class Program
    {
        class C     //創建一個類C
        {
            public int Value = 0;  //聲明一個公共int類型的變量Value
        }
        static void Main(string[] args)
        {
            int v1 = 0;     //聲明一個int類型的變量v1,並初始化爲0
            int v2 = v1;    //聲明一個int類型的變量v2,並將v1賦值給v2
            v2 = 927;       //重新將變量v2賦值爲927
            C r1 = new C(); //使用new關鍵字創建引用對象
            C r2 = r1;      //使r1等r2
            r2.Value = 112; //設置變量r2的Value值
            Console.WriteLine("Values:{0},{1}", v1, v2);    //輸出變量v1和v2
            Console.WriteLine("Refs:{0},{1}", r1.Value, r2.Value);  //輸出引用類型對象的Value值
            Console.ReadLine();
        }
    }
}


                     枚舉類型


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace meijuleixing
{
    class Program
    {
        enum MyDate      //使用enum創建枚舉
        { 
            Sun=0,        //設置枚舉名稱爲Sun,枚舉值爲0  
            Mon=1,       //設置枚舉名稱爲Mon,枚舉值爲1
            Tue=2,       //設置枚舉名稱爲Tue,枚舉值爲2 
            Wed=3,      //設置枚舉名稱爲Wed,枚舉值爲3
            Thi=4,      //設置枚舉名稱爲Thi,枚舉值爲4 
            Fri=5,     //設置枚舉名稱爲Fri,枚舉值爲5
            Sat=6      //設置枚舉名稱爲Sat,枚舉值爲6 
        }
        static void Main(string[] args)
        {
            int k = (int)DateTime.Now.DayOfWeek;    //獲取代表星期幾的返回值
            switch (k)
            {
                //如果k等於沒記變量MyDate中的Sun的枚舉值,則輸出今天是星期日
                case (int)MyDate.Sun: Console.WriteLine("今天是星期日"); break;
                //如果k等於沒記變量MyDate中的Sun的枚舉值,則輸出今天是星期日
                case (int)MyDate.Mon: Console.WriteLine("今天是星期一"); break;
                //如果k等於沒記變量MyDate中的Sun的枚舉值,則輸出今天是星期日
                case (int)MyDate.Tue: Console.WriteLine("今天是星期二"); break;
                //如果k等於沒記變量MyDate中的Sun的枚舉值,則輸出今天是星期日
                case (int)MyDate.Wed: Console.WriteLine("今天是星期三"); break;
                //如果k等於沒記變量MyDate中的Sun的枚舉值,則輸出今天是星期日
                case (int)MyDate.Thi: Console.WriteLine("今天是星期四"); break;
                //如果k等於沒記變量MyDate中的Sun的枚舉值,則輸出今天是星期日
                case (int)MyDate.Fri: Console.WriteLine("今天是星期五"); break;
                //如果k等於沒記變量MyDate中的Sun的枚舉值,則輸出今天是星期日
                case (int)MyDate.Sat: Console.WriteLine("今天是星期六"); break;
            }
            Console.ReadLine();
        }
    }
}




            值類型和引用類型的簡單區別
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ReferenceAndValue.Demonstration();//調用ReferenceAndValue類中的Demonstration方法
            Console.ReadLine();
        }
    }
    public class stamp              //定義一個類
    {
        public string Name { get; set; } //定義一個引用類型
        public int Age { get; set; }  //定義一個值類型
    }
    public static class ReferenceAndValue    //定義一個靜態類
    {
        public static void Demonstration()   //定義一個靜態方法
        {
            stamp stamp_1 = new stamp { Name = "Premiere", Age = 25 }; //實例化
            stamp stamp_2 = new stamp { Name = "Again", Age = 47 };    //實例化
            int age = stamp_1.Age;                                     //獲取值類型Age的值
            stamp_1.Age = 22;                                          //修改值類型的值
            stamp guru = stamp_2;                                      //獲取stamp_2中的值
            stamp_2.Name = "Again Amend";                              //修改引用類型Name的值
            Console.WriteLine("stamp_1's age:{0}", stamp_1.Age);       //顯示stamp_1中的值
            Console.WriteLine("age's value:{0}", age);                 //顯示age的值
            Console.WriteLine("stamp_2's name:{0}", stamp_2.Name);     //顯示stamp_2中的Name值
            Console.WriteLine("guru's name:{0}", guru.Name);           //顯示guru中的Name值
        }


    }
}




         類型轉換


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ZhuanHuan
{
    class Program
    {
        static void Main(string[] args)
        {
               //類型轉換之顯示轉換(強制轉換)
            //double x = 19910310.0425;      //建立double類型變量x
            //int y = (int)x;                //顯示轉換成整型變量y
            //Console.WriteLine(y);          //輸出整型變量y
            //Console.ReadLine();




            double x = 19910310.0425;        //建立double類型變量x
            int y = Convert.ToInt32(x);      //通過Convert關鍵字轉換
            Console.WriteLine(y);            //輸出整型變量y
            Console.ReadLine();
        }
    }
}


     
                 裝箱與拆箱
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ZhuangXiangAndChaiXiang
{
    class Program
    {
        static void Main(string[] args)
        {
               //裝箱
            //int i = 2008;             //聲明一個int類型變量爲i,並初始化爲2008
            //object obj = i;           //聲明一個object類型爲obj,並初始化值爲i
            //Console.WriteLine("1.i的值爲{0},裝箱之後的對象爲{1}", i, obj);
            //i = 927;                  //重新將i賦值爲927
            //Console.WriteLine("2.i的值爲{0},裝箱之後的對象爲{1}", i, obj);
            //Console.ReadLine();


               //拆箱
            int i = 112;                                //聲明一個int類型的變量爲i,並初始化爲112
            object obj = i;                             //執行裝箱操作
            Console.WriteLine("裝箱操作:值爲{0},裝箱之後對象爲{1}", i, obj);
            int j = (int)obj;                           //執行拆箱操作
            Console.WriteLine("拆箱操作:裝箱對象爲{0},值爲{1}", obj, j);
            Console.ReadLine();
        }
    }
}








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