C#相關(guān)試題參考模板_第1頁
C#相關(guān)試題參考模板_第2頁
C#相關(guān)試題參考模板_第3頁
C#相關(guān)試題參考模板_第4頁
C#相關(guān)試題參考模板_第5頁
已閱讀5頁,還剩65頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、1 / 701. 傳入某個屬性的set方法的隱含參數(shù)的名稱是什么?value,它的類型和屬性所聲名的類型相同。 2. 如何在C#中實現(xiàn)繼承?在類名后加上一個冒號,再加上基類的名稱。 3. C#支持多重繼承么?不支持??梢杂媒涌趤韺崿F(xiàn)。 4. 被protected修飾的屬性/方法在何處可以訪問?在繼承或間接繼承與這個類的子類中可以訪問。 5. 私有成員會被繼承么?會,但是不能被訪問。所以看上去他們似乎是不能被繼承的,但實際上確實被繼承了。 6. 請描述一下修飾符protected internal。 被protected interna

2、l修飾的屬性/方法只能在它的在同一個程序集(Assembly)中的子類被訪問。 7. C#提供一個默認(rèn)的無參數(shù)構(gòu)造函數(shù),當(dāng)我實現(xiàn)了另外一個有一個參數(shù)的構(gòu)造函數(shù)時候,還想保留這個無參數(shù)的構(gòu)造函數(shù)。這樣我應(yīng)該寫幾個構(gòu)造函數(shù)?兩個,一旦你實現(xiàn)了一個構(gòu)造函數(shù),C#就不會再提供默認(rèn)的構(gòu)造函數(shù)了,所以需要手動實現(xiàn)那個無參數(shù)構(gòu)造函數(shù)。 8. C#中所有對象共同的基類是什么?System.Object. 9. 重載和覆寫有什么區(qū)別?重載提供了對一個方法簽名的不同參數(shù)調(diào)用的實現(xiàn)。覆寫提供了子類中改變父類方法行為的實現(xiàn)。 10. 在方法定義中,virtual有什么含意? 被

3、virtual修飾的方法可以被子類覆寫。 11. 能夠?qū)⒎庆o態(tài)的方法覆寫成靜態(tài)方法么?不能,覆寫方法的簽名必須與被覆寫方法的簽名保持一致,除了將virtual改為override。 12. 可以覆寫私有的虛方法么?不可以,甚至子類中無法訪問父類中的私有方法。 13. 能夠阻止某一個類被其他類繼承么? 可以,使用關(guān)鍵字sealed。 14. 能夠?qū)崿F(xiàn)允許某個類被繼承,但不允許其中的某個方法被覆寫么? 可以,標(biāo)記這個類為public,并標(biāo)記這個方法為sealed。 15. 什么是抽象類(abstract class)?一種不可以被實例化的類。抽象類

4、中一般含有抽象方法,當(dāng)然也可有具體實現(xiàn)。繼承類只有實現(xiàn)過所有抽象類的抽象方法后才能被實例化。 16. 何時必須聲明一個類為抽象類? 當(dāng)這個類中包含抽象方法時,或是該類并沒有完全實現(xiàn)父類的抽象方法時。 17. 接口(interface)是什么?只含有共有抽象方法(public abstract method)的類。這些方法必須在子類中被實現(xiàn)。 18. 為什么不能指定接口中方法的修飾符? 接口中的方法用來定義對象之間通信的契約,指定接口中的方法為私有或保護沒有意義。他們默認(rèn)為公有方法。 19. 可以繼承多個接口么? 當(dāng)然。 20. 那么如果這些接口

5、中有重復(fù)的方法名稱呢? 這種情況中你可以決定如何實現(xiàn)。當(dāng)然需要特別得小心。但是在編譯環(huán)節(jié)是沒有問題的。 21. 接口和抽象類的區(qū)別是什么? 接口中所有方法必須是抽象的,并且不能指定方法的訪問修飾符。抽象類中可以有方法的實現(xiàn),也可以指定方法的訪問修飾符。 22. 如何區(qū)別重載方法? 不同的參數(shù)類型,不同的參數(shù)個數(shù),不同的參數(shù)順序。 23. const和readonly有什么區(qū)別?const關(guān)鍵字用來聲明編譯時常量,readonly用來聲明運行時常量。 24. System.String 和System.StringBuilder有什么區(qū)別?System.S

6、tring是不可變的字符串。System.StringBuilder存放了一個可變的字符串,并提供一些對這個字符串修改的方法。 1.靜態(tài)成員和非靜態(tài)成員的區(qū)別?2.const 和 static readonly 區(qū)別?3.extern 是什么意思?4.abstract 是什么意思?5.internal 修飾符起什么作用?6.sealed 修飾符是干什么的?7.override 和 overload 的區(qū)別?8.什么是索引指示器?9.new 修飾符是起什么作用?10.this 關(guān)鍵字的含義?11.可以使用抽象函數(shù)重寫基類中的虛函數(shù)嗎?12.密封類可以有虛函數(shù)嗎?13.什么是屬性訪問器?14.ab

7、stract 可以和 virtual 一起使用嗎?可以和 override 一起使用嗎?15.接口可以包含哪些成員?16.類和結(jié)構(gòu)的區(qū)別?17.接口的多繼承會帶來哪些問題?18.抽象類和接口的區(qū)別?19.別名指示符是什么?20.如何手工釋放資源?21.P/Invoke是什么?22.StringBuilder 和 String 的區(qū)別?23.explicit 和 implicit 的含義?24.params 有什么用?25.什么是反射? 以下是我做的一份參考答案(C# 語言范疇之內(nèi)),如果有不準(zhǔn)確、不全面的,歡迎各位朋友指正! 1.靜態(tài)成員和非靜態(tài)成員的區(qū)別?答:靜態(tài)變量使用 stat

8、ic 修飾符進行聲明,在類被實例化時創(chuàng)建,通過類進行訪問不帶有 static 修飾符聲明的變量稱做非靜態(tài)變量,在對象被實例化時創(chuàng)建,通過對象進行訪問一個類的所有實例的同一靜態(tài)變量都是同一個值,同一個類的不同實例的同一非靜態(tài)變量可以是不同的值靜態(tài)函數(shù)的實現(xiàn)里不能使用非靜態(tài)成員,如非靜態(tài)變量、非靜態(tài)函數(shù)等示例:using System;using System.Collections.Generic;using System.Text; namespace Example01    class Program     

9、       class Class1                    public static String staticStr = "Class"            public String notstaticS

10、tr = "Obj"                static void Main(string args)                    /靜態(tài)變量通過類進行訪問,該類所有實例的同一靜態(tài)變量都是同一個值    

11、;        Console.WriteLine("Class1's staticStr: 0", Class1.staticStr);             Class1 tmpObj1 = new Class1();            tmpObj1.not

12、staticStr = "tmpObj1"            Class1 tmpObj2 = new Class1();            tmpObj2.notstaticStr = "tmpObj2"          

13、60;  /非靜態(tài)變量通過對象進行訪問,不同對象的同一非靜態(tài)變量可以有不同的值            Console.WriteLine("tmpObj1's notstaticStr: 0", tmpObj1.notstaticStr);            Console.WriteLine("tmpObj2's n

14、otstaticStr: 0", tmpObj2.notstaticStr);             Console.ReadLine();            結(jié)果:Class1's staticStr: ClasstmpObj1's notstaticStr: tmpObj1tmpObj2's notstaticStr: tmpObj2

15、2.const 和 static readonly 區(qū)別?答:const用 const 修飾符聲明的成員叫常量,是在編譯期初始化并嵌入到客戶端程序static readonly用 static readonly 修飾符聲明的成員依然是變量,只不過具有和常量類似的使用方法:通過類進行訪問、初始化后不可以修改。但與常量不同的是這種變量是在運行期初始化示例:測試類:using System;using System.Collections.Generic;using System.Text; namespace Example02Lib    public c

16、lass Class1            public const String strConst = "Const"        public static readonly String strStaticReadonly = "StaticReadonly"        /public const Str

17、ing strConst = "Const Changed"        /public static readonly String strStaticReadonly = "StaticReadonly Changed"     客戶端代碼:using System;using System.Collections.Generic;using System.Text;using Example02Lib; namespace Ex

18、ample02    class Program            static void Main(string args)                    /修改Example02中Class1的strConst初始值后,只編譯Example02Lib項目 

19、0;          /然后到資源管理器里把新編譯的Example02Lib.dll拷貝Example02.exe所在的目錄,執(zhí)行Example02.exe            /切不可在IDE里直接調(diào)試運行因為這會重新編譯整個解決方案!             /可以看到s

20、trConst的輸出沒有改變,而strStaticReadonly的輸出已經(jīng)改變            /表明Const變量是在編譯期初始化并嵌入到客戶端程序,而StaticReadonly是在運行時初始化的            Console.WriteLine("strConst : 0", Class1.strConst);  &

21、#160;         Console.WriteLine("strStaticReadonly : 0", Class1.strStaticReadonly);             Console.ReadLine();            結(jié)果:strConst :

22、 ConststrStaticReadonly : StaticReadonly 修改后的示例:測試類:using System;using System.Collections.Generic;using System.Text; namespace Example02Lib    public class Class1            /public const String strConst = "Const" &

23、#160;      /public static readonly String strStaticReadonly = "StaticReadonly"        public const String strConst = "Const Changed"        public static readonly String strStaticReadon

24、ly = "StaticReadonly Changed"    結(jié)果strConst : ConststrStaticReadonly : StaticReadonly Changed3.extern 是什么意思?答:extern 修飾符用于聲明由程序集外部實現(xiàn)的成員函數(shù)經(jīng)常用于系統(tǒng)API函數(shù)的調(diào)用(通過 DllImport )。注意,和DllImport一起使用時要加上 static 修飾符也可以用于對于同一程序集不同版本組件的調(diào)用(用 extern 聲明別名)不能與 abstract 修飾符同時使用示例:using System;usin

25、g System.Collections.Generic;using System.Text;using System.Runtime.InteropServices; namespace Example03    class Program            /注意DllImport是一個Attribute Property,在System.Runtime.InteropServices命名空間中定義    &

26、#160;   /extern與DllImport一起使用時必須再加上一個static修飾符        DllImport("User32.dll")        public static extern int MessageBox(int Handle, string Message, string Caption, int Type);      

27、;   static int Main()                    string myString;            Console.Write("Enter your message: ");    

28、0;       myString = Console.ReadLine();            return MessageBox(0, myString, "My Message Box", 0);            結(jié)果: 4.abstract 是什么意思?答:abstract 修飾符

29、可以用于類、方法、屬性、事件和索引指示器(indexer),表示其為抽象成員abstract 不可以和 static 、virtual 一起使用聲明為 abstract 成員可以不包括實現(xiàn)代碼,但只要類中還有未實現(xiàn)的抽象成員(即抽象類),那么它的對象就不能被實例化,通常用于強制繼承類必須實現(xiàn)某一成員示例:using System;using System.Collections.Generic;using System.Text; namespace Example04    #region 基類,抽象類    public

30、 abstract class BaseClass            /抽象屬性,同時具有g(shù)et和set訪問器表示繼承類必須將該屬性實現(xiàn)為可讀寫        public abstract String Attribute                 

31、0;  get;            set;                 /抽象方法,傳入一個字符串參數(shù)無返回值        public abstract void Function(String value);   

32、      /抽象事件,類型為系統(tǒng)預(yù)定義的代理(delegate):EventHandler        public abstract event EventHandler Event;         /抽象索引指示器,只具有g(shù)et訪問器表示繼承類必須將該索引指示器實現(xiàn)為只讀        public abstract

33、Char thisint Index                    get;                #endregion     #region 繼承類    public class DeriveCla

34、ss : BaseClass            private String attribute;         public override String Attribute                    get 

35、;                           return attribute;                      

36、60; set                            attribute = value;                     &

37、#160;      public override void Function(String value)                    attribute = value;            if (Event != null) &#

38、160;                          Event(this, new EventArgs();                      

39、      public override event EventHandler Event;        public override Char thisint Index                    get      &#

40、160;                     return attributeIndex;                            #endregion

41、     class Program            static void OnFunction(object sender, EventArgs e)                    for (int i = 0; i < (DeriveClass)send

42、er).Attribute.Length; i+)                            Console.WriteLine(DeriveClass)sender)i);             

43、0;              static void Main(string args)                    DeriveClass tmpObj = new DeriveClass();       

44、0;     tmpObj.Attribute = "1234567"            Console.WriteLine(tmpObj.Attribute);             /將靜態(tài)函數(shù)OnFunction與tmpObj對象的Event事件進行關(guān)聯(lián)   

45、0;        tmpObj.Event += new EventHandler(OnFunction);             tmpObj.Function("7654321");             Console.ReadLine(); &#

46、160;          結(jié)果:12345677654321 5.internal 修飾符起什么作用?答:internal 修飾符可以用于類型或成員,使用該修飾符聲明的類型或成員只能在同一程集內(nèi)訪問接口的成員不能使用 internal 修飾符值得注意的是,如果為 internal 成員加上了 protected 修飾符,這時的訪問級別為 internal 或 protected。只是看字面意思容易弄錯,許多人認(rèn)為 internal protected 應(yīng)該是“只有同一個程序集中的子類可以訪問”,但其實它表示“同一

47、個程序集中的所有類,以及所有程序集中的子類都可以訪問”示例Example05Lib 項目的 Class1 using System;using System.Collections.Generic;using System.Text; namespace Example05Lib    public class Class1            internal String strInternal = null;   &#

48、160;    public String strPublic;        internal protected String strInternalProtected = null;    結(jié)果Example05Lib 項目的 Class2 類可以訪問到 Class1 的 strInternal 成員,當(dāng)然也可以訪問到 strInternalProtected 成員,因為他們在同一個程序集里 Example05 項目里的 Class3 類無法訪問到

49、Class1 的 strInternal 成員,因為它們不在同一個程序集里。但卻可以訪問到 strInternalProtected 成員,因為 Class3 是 Class1 的繼承類 Example05 項目的 Program 類既無法訪問到 Class1 的 strInternal 成員,也無法訪問到 strInternalProtected 成員,因為它們既不在同一個程序集里也不存在繼承關(guān)系 6.sealed 修飾符是干什么的?答:sealed 修飾符表示密封用于類時,表示該類不能再被繼承,不能和 abstract 同時使用,因為這兩個修飾符在含義上互相排斥用于方法

50、和屬性時,表示該方法或?qū)傩圆荒茉俦恢貙?,必須?override 關(guān)鍵字一起使用,因為使用 sealed 修飾符的方法或?qū)傩钥隙ㄊ腔愔邢鄳?yīng)的虛成員通常用于實現(xiàn)第三方類庫時不想被客戶端繼承,或用于沒有必要再繼承的類以防止濫用繼承造成層次結(jié)構(gòu)體系混亂恰當(dāng)?shù)睦?sealed 修飾符也可以提高一定的運行效率,因為不用考慮繼承類會重寫該成員示例:using System;using System.Collections.Generic;using System.Text; namespace Example06    class Program &#

51、160;          class A                    public virtual void F()                  

52、60;         Console.WriteLine("A.F");                        public virtual void G()         &#

53、160;                  Console.WriteLine("A.G");                            class B :

54、A                    public sealed override void F()                           

55、Console.WriteLine("B.F");                        public override void G()                  

56、0;         Console.WriteLine("B.G");                            class C : B         &

57、#160;          public override void G()                            Console.WriteLine("C.G");    

58、;                        static void Main(string args)                    new A().F();   &

59、#160;        new A().G();            new B().F();            new B().G();            new C().F();

60、60;           new C().G();             Console.ReadLine();            結(jié)果:類 B 在繼承類 A 時可以重寫兩個虛函數(shù),如圖所示: 由于類 B 中對 F 方法進行了密封, 類 C 在繼承類 B 時只能

61、重寫一個函數(shù),如圖所示: 控制臺輸出結(jié)果,類 C 的方法 F 只能是輸出 類B 中對該方法的實現(xiàn):A.FA.GB.FB.GB.FC.G 7.override 和 overload 的區(qū)別?答:override 表示重寫,用于繼承類對基類中虛成員的實現(xiàn)overload 表示重載,用于同一個類中同名方法不同參數(shù)(包括類型不同或個數(shù)不同)的實現(xiàn)示例:using System;using System.Collections.Generic;using System.Text; namespace Example07    class Program&

62、#160;           class BaseClass                    public virtual void F()                

63、            Console.WriteLine("BaseClass.F");                            class DeriveClass : BaseClass &#

64、160;                  public override void F()                            base.F(); &

65、#160;              Console.WriteLine("DeriveClass.F");                        public void Add(int Left, int Right)&

66、#160;                           Console.WriteLine("Add for Int: 0", Left + Right);              

67、0;         public void Add(double Left, double Right)                            Console.WriteLine("Add for int: 0", Lef

68、t + Right);                            static void Main(string args)                  

69、0; DeriveClass tmpObj = new DeriveClass();            tmpObj.F();            tmpObj.Add(1, 2);            tmpObj.Add(1.1, 2.2); &#

70、160;           Console.ReadLine();            結(jié)果:BaseClass.FDeriveClass.FAdd for Int: 3Add for int: 3.3 8.什么是索引指示器?答:實現(xiàn)索引指示器(indexer)的類可以象數(shù)組那樣使用其實例后的對象,但與數(shù)組不同的是索引指示器的參數(shù)類型不僅限于int簡單來說,其本質(zhì)就是一個含參數(shù)屬性示例: us

71、ing System;using System.Collections.Generic;using System.Text; namespace Example08    public class Point            private double x, y;        public Point(double X, double Y)   &

72、#160;                x = X;            y = Y;                /重寫ToString方法方便輸出    

73、60;   public override string ToString()                    return String.Format("X: 0 , Y: 1", x, y);                public c

74、lass Points            Point points;        public Points(Point Points)                    points = Points;   

75、             public int PointNumber                    get                  

76、;           return points.Length;                                 /實現(xiàn)索引訪問器      

77、  public Point thisint Index                    get                          

78、60; return pointsIndex;                             /感謝watson hua(    /索引指示器的實質(zhì)是含參屬性,參數(shù)并不只限于int    class WeatherOfWeek     &#

79、160;      public string thisint Index                    get                     

80、0;      /注意case段使用return直接返回所以不需要break                switch (Index)                       

81、;             case 0:                                     &#

82、160;              return "Today is cloudy!"                               &

83、#160;            case 5:                                     

84、0;              return "Today is thundershower!"                              &#

85、160;             default:                                    

86、0;               return "Today is fine!"                               

87、;                                     public string thisstring Day            &#

88、160;       get                            string TodayWeather = null;            

89、;    /switch的標(biāo)準(zhǔn)寫法                switch (Day)                            &#

90、160;       case "Sunday":                                        

91、            TodayWeather = "Today is cloudy!"                            break;   

92、60;                                        case "Friday":       &

93、#160;                                            TodayWeather = "Today is thundershow

94、er!"                            break;                    &#

95、160;                       default:                           

96、60;                        TodayWeather = "Today is fine!"                   

97、60;        break;                                           

98、             return TodayWeather;                            class Program        

99、60;   static void Main(string args)                    Point tmpPoints = new Point10;            for (int i = 0; i < tmpPoints.Length; i+)

100、0;                           tmpPointsi = new Point(i, Math.Sin(i);                   

101、;      Points tmpObj = new Points(tmpPoints);            for (int i = 0; i < tmpObj.PointNumber; i+)                    &#

102、160;       Console.WriteLine(tmpObji);                          string Week = new string "Sunday", "Monday", "Tuesday",

103、"Wednesday", "Thursday", "Friday", "Staurday"            WeatherOfWeek tmpWeatherOfWeek = new WeatherOfWeek();            for (int i = 0; i < 6; i+

104、)                            Console.WriteLine(tmpWeatherOfWeeki);                  &

105、#160;     foreach (string tmpDay in Week)                            Console.WriteLine(tmpWeatherOfWeektmpDay);      

106、60;                  Console.ReadLine();            結(jié)果:X: 0 , Y: 0X: 1 , Y: 0.841470984807897X: 2 , Y: 0.909297426825682X: 3 , Y: 0.141120008059867X: 4 , Y: -0.7568024953

107、07928X: 5 , Y: -0.958924274663138X: 6 , Y: -0.279415498198926X: 7 , Y: 0.656986598718789X: 8 , Y: 0.989358246623382X: 9 , Y: 0.412118485241757Today is cloudy!Today is fine!Today is fine!Today is fine!Today is fine!Today is thundershower!Today is cloudy!Today is fine!Today is fine!Today is fine!Today

108、 is fine!Today is thundershower!Today is fine! 9.new 修飾符是起什么作用?答:new 修飾符與 new 操作符是兩個概念new 修飾符用于聲明類或類的成員,表示隱藏了基類中同名的成員。而new 操作符用于實例化一個類型new 修飾符只能用于繼承類,一般用于彌補基類設(shè)計的不足new 修飾符和 override 修飾符不可同時用在一個成員上,因為這兩個修飾符在含義上互相排斥示例:using System;using System.Collections.Generic;using System.Text; namespace

109、Example09    class BaseClass            /基類設(shè)計者聲明了一個PI的公共變量,方便進行運算        public static double PI = 3.1415;        class DervieClass : BaseClass     &#

110、160;      /繼承類發(fā)現(xiàn)該變量的值不能滿足運算精度,于是可以通過new修飾符顯式隱藏基類中的聲明        public new static double PI = 3.1415926;        class Program            static void Main(string arg

111、s)                    Console.WriteLine(BaseClass.PI);            Console.WriteLine(DervieClass.PI);         &

112、#160;   Console.ReadLine();            結(jié)果:3.14153.1415926 10.this 關(guān)鍵字的含義?答:this 是一個保留字,僅限于構(gòu)造函數(shù)和方法成員中使用在類的構(gòu)造函數(shù)中出現(xiàn)表示對正在構(gòu)造的對象本身的引用,在類的方法中出現(xiàn)表示對調(diào)用該方法的對象的引用,在結(jié)構(gòu)的構(gòu)造上函數(shù)中出現(xiàn)表示對正在構(gòu)造的結(jié)構(gòu)的引用,在結(jié)構(gòu)的方法中出現(xiàn)表示對調(diào)用該方法的結(jié)果的引用this 保留字不能用于靜態(tài)成員的實現(xiàn)里,因為這時對象或結(jié)構(gòu)并未實例化在 C#

113、系統(tǒng)中,this 實際上是一個常量,所以不能使用 this+ 這樣的運算this 保留字一般用于限定同名的隱藏成員、將對象本身做為參數(shù)、聲明索引訪問器、判斷傳入?yún)?shù)的對象是否為本身示例:using System;using System.Collections.Generic;using System.Text; namespace Example10    class Class1            private double c; &#

114、160;      private string value;         public double C                    get           

115、                return c;                            public Class1(double c)    

116、                /限定同名的隱藏成員            this.c = c;                public Class1(Class1 value)  &

117、#160;                 /用對象本身實例化自己沒有意義            if (this != value)                            c = value.C;         

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論