當前位置:學問君>學習教育>考研>

筆試題目(綜合版樣題)

學問君 人氣:1.56W
題記:一年一度的招聘黃金時間來臨了,本人決定整理C#的資料爲本人和園子裏的朋友共享!
C#資料(一)
1.靜態成員和非靜態成員的區別?
答:
靜態變量使用 static 修飾符進行聲明,在類被實例化時創建,透過類進行訪問不帶有 static 修飾符聲明的變量稱做非靜態變量,在對象被實例化時創建,透過對象進行訪問一個類的所有實例的同一靜態變量都是同一個值,同一個類的不同實例的同一非靜態變量可以是不同的值靜態函數的實現裏不能使用非靜態成員,如非靜態變量、非靜態函數等
示例:
using System;
using ric;
using ;
 
namespace Example01
{
    class Program
    {
        class Class1
        {
            public static String staticStr = "Class";
            public String notstaticStr = "Obj";
        }
        static void Main(string[] args)
        {
            //靜態變量透過類進行訪問,該類所有實例的同一靜態變量都是同一個值
            eLine("Class1's staticStr: {0}", icStr);
 
            Class1 tmpObj1 = new Class1();
            taticStr = "tmpObj1";
            Class1 tmpObj2 = new Class1();
            taticStr = "tmpObj2";
 
            //非靜態變量透過對象進行訪問,不同對象的同一非靜態變量可以有不同的值
            eLine("tmpObj1's notstaticStr: {0}", taticStr);
            eLine("tmpObj2's notstaticStr: {0}", taticStr);
 
            Line();
        }
    }
}

結果:
Class1's staticStr: Class
tmpObj1's notstaticStr: tmpObj1
tmpObj2's notstaticStr: tmpObj2

筆試題目(綜合版樣題)

t 和 static readonly 區別?

答:const

用 const 修飾符聲明的成員叫常量,是在編譯期初始化並嵌入到客戶端程序

static readonly

用 static readonly 修飾符聲明的成員依然是變量,只不過具有和常量類似的使用方法:透過類進行訪問、初始化後不可以修改。但與常量不同的是這種變量是在執行期初始化

示例:

測試類:

using System;
using ric;
using ;
 
namespace Example02Lib
{
    public class Class1
    {
        public const String strConst = "Const";
        public static readonly String strStaticReadonly = "StaticReadonly";
        //public const String strConst = "Const Changed";
        //public static readonly String strStaticReadonly = "StaticReadonly 
Changed";
    }
}
 
客戶端代碼:
using System;
using ric;
using ;
using Example02Lib;
 
namespace Example02
{
    class Program
    {
        static void Main(string[] args)
        {
            //修改Example02中Class1的strConst初始值後,只編譯Example02Lib項目
            //然後到資源管理器裏把新編譯的拷貝所在的目錄,
執行
            //切不可在IDE裏直接調試執行因爲這會重新編譯整個解決方案!!
 
            //可以看到strConst的輸出沒有改變,而strStaticReadonly的輸出已經改變
            //表明Const變量是在編譯期初始化並嵌入到客戶端程序,而StaticReadonly是在執行時初始化的
            eLine("strConst : {0}", onst);
            eLine("strStaticReadonly : {0}", taticReadonly);
 
            Line();
        }
    }
}

結果:
strConst : Const
strStaticReadonly : StaticReadonly

修改後的示例:

測試類:

using System;
using ric;
using ;
 
namespace Example02Lib
{
    public class Class1
    {
        //public const String strConst = "Const";
        //public static readonly String strStaticReadonly = "StaticReadonly";
        public const String strConst = "Const Changed";
        public static readonly String strStaticReadonly = "StaticReadonly Changed";
    }
}

結果

strConst : Const
strStaticReadonly : StaticReadonly Changed

rn 是什麼意思?
答:extern 修飾符用於聲明由程序集外部實現的成員函數經常用於系統API函數的調用(透過 DllImport )。注意,和DllImport一起使用時要加上 static 修飾符也可以用於對於同一程序集不同版本組件的調用(用 extern 聲明別名)不能與 abstract 修飾符同時使用

示例:

using System;
using ric;
using ;
using ropServices;
 
namespace Example03
{
    class Program
    {
        //注意DllImport是一個Attribute Property,在ropServices命名空間中定義
        //extern與DllImport一起使用時必須再加上一個static修飾符
        [DllImport("")]

public static extern int MessageBox(int Handle, string Message,

string Caption, int Type);

 
        static int Main()
        {
            string myString;
            e("Enter your message: ");
            myString = Line();
            return MessageBox(0, myString, "My Message Box", 0);