目錄
C# OOP 面試問題簡介
第 1 部分 – C# OOP 面試問題(基礎)
1.介面和抽象類別有什麼差別?
2.什麼是代表及其用途?
3.後期綁定和早期綁定有什麼不同?
4.如果繼承的介面的方法名稱衝突會發生什麼事?
第 2 部分 – C# OOP 面試問題(進階)
6.什麼是可訪問性修飾符,C# 有多少種?
7. What is a virtual method in C#?
8. How to avoid NULL in C#?
9. What is the extension method in C#, and how to use them?
10. Can “this” keyword be used within a static method?
首頁 後端開發 C#.Net教程 C# OOP 面試問題

C# OOP 面試問題

Sep 03, 2024 pm 03:35 PM
c# c# tutorial

C# OOP 面試問題簡介

C# 是一種物件導向、函數式、通用和元件導向的程式語言。它已被用來創建各種應用程式;它特別擅長建立 Windows 桌面應用程式和遊戲。 Web 開發也可以使用 C# 有效完成,C# 在行動開發中越來越流行。因此,對於任何希望開發網頁和遊戲的程式設計師來說,它是一個絕佳的選擇。靜態類型語言在編寫的原始程式碼成為應用程式之前會對其進行徹底檢查。這是一種學習起來很複雜的語言,掌握它可能需要大量的時間。開發者可以使用各種跨平台工具用 C# 創建可在行動和桌面平台上使用的應用程式。

如果您正在尋找與 C# OOP 相關的工作,您必須準備 2023 年 C# OOP 面試問題。雖然每次面試的情況不同,工作範圍也不同,但我們可以幫助您解答最熱門的 C# OOP 面試問題並附上答案,這將幫助您實現飛躍並在面試中獲得成功。

第 1 部分 – C# OOP 面試問題(基礎)

第一部分涵蓋基本的 C# OOP 面試問題和答案

1.介面和抽象類別有什麼差別?

答案:

一些差異如下:

  • 抽象類別可以有非抽象方法(具體方法),而在介面情況下,所有形式都必須是抽象的。
  • 抽象類別可以宣告或使用變量,而介面則不能。
  • 在抽象類別中,所有資料成員或函數預設都是私有的,而在介面中,所有資料成員或函數都是公共的;我們無法手動更改它們。
  • 抽象類別使用建構函數,而介面中沒有任何建構子。
  • 一個類別可以實現任意數量的接口,但子類別最多只能使用一個抽象類別。

2.什麼是代表及其用途?

答案:

委託物件是一個引用型別變量,它保存對方法的參考。連接可以在運行時更改,該連接由委託的物件擁有。一個委託物件可以有很多函數引用,也稱為呼叫列表,它們引用順序 FIFO 中的任務;我們可以在執行時間透過 += 運算子在此列表中新增函數 ref 並透過 -= 運算子刪除。

讓我們繼續討論以下 C# OOP 面試問題。

3.後期綁定和早期綁定有什麼不同?

答案:

在編譯時多型或早期綁定中,我們會使用多個同名但參數型別不同或多個參數的方法。正因為如此,我們可以在同一個類別中使用相同的方法名稱執行不同的任務,也稱為方法重載。

public class TestData
{
public int Add(int a, int b, int c)
{
return a + b + c;
}
public int Add(int a, int b)
{
return a + b;
}
}
class Program
{
static void Main(string[] args)
{
TestData dataClass = new TestData();
int add2 = dataClass.Add(45, 34, 67);
int add1 = dataClass.Add(23, 34);
}
}

動態或運行時多態性的另一個名稱是後期綁定。這裡,方法名稱和方法簽名(參數數量和參數類型必須相同,並且可能有不同的實作)。方法重寫是動態多態性的一個例子。

public class Drawing
{
public virtual double Area()
{
return 0;
}
}
public class Square : Drawing
{
public double Length { get; set; }
public Square()
{
Length = 6;
}
public override double Area()
{
return Math.Pow(Length, 2);
}
}
public class Rectangle : Drawing
{
public double Height { get; set; }
public double Width { get; set; }
public Rectangle()
{
Height = 5.3;
Width = 3.4;
}
public override double Area()
{
return Height * Width;
}
}
class Program
{
static void Main(string[] args)
{
Drawing square = new Square();
Console.WriteLine("Area :" + square.Area());
Drawing rectangle = new Rectangle();
Console.WriteLine("Area :" + rectangle.Area());
}
}

4.如果繼承的介面的方法名稱衝突會發生什麼事?

答案:

這些是一些典型的 C# OOP 面試問題。想像一下同一個類別包含有衝突的方法。在這種情況下,由於相同的名稱和相同的簽名,我們無法在同一個類別中獨立實作它們的主體,因此我們必須在方法名稱之前使用介面名稱來消除這種方法混亂。

interface testInterface1 {<br> void Show();<br> }<br> interface testInterface2 {<br> void Show();<br> }<br> class Abc: testInterface1,<br> testInterface2 {<br> void testInterface1.Show() {<br> Console.WriteLine("For testInterface1 !!");<br> }<br> void testInterface2.Show() {<br> Console.WriteLine("For testInterface2 !!");<br> }<br> }<br>

第 2 部分 – C# OOP 面試問題(進階)

第一部分涵蓋高級 C# OOP 面試問題和答案

6.什麼是可訪問性修飾符,C# 有多少種?

答案:

存取修飾符是用於指定成員或類型聲明的可存取性的關鍵字。在C#中,存取修飾符有5種類型。

公共 – 存取公共成員沒有限制。

私有-類別定義內的有限存取;如果未指定,則這是預設值。

受保護 - 存取僅限於類別定義內以及從課程繼承的任何類別。

內部 - 存取僅限於目前項目定義的類別。

7. What is a virtual method in C#?

Answer:

Developers use a virtual method to define a method that can be redefined in derived classes. We make a virtual method in the base class using the virtual keyword, and that method is overridden in the derived class using the override keyword.

Let us move on to the following C# OOP Interview Questions.

8. How to avoid NULL in C#?

Answer:

Null is not an object. We can have a class, but a variable with a NULL value is not pointing to any object. We might come across a piece of code that contains many conditional statements that check if the value of a variable is NULL. Let’s review a static method:

public static string DisplayUpperString( string s ){
string upper = string.Empty;
If( s == null ) {
upper = null;
}
else {
upper = s.ToUpper();
}
return upper;
}

This code is fine and converts to the upper case of a given string.
But from an OO perspective, consider constructing an object that represents nothing rather than evaluating it for NULL.

public static String DisplayUpperString ( PossibleString s ){
string upper = s.ToPossibleUpper();
return upper;
}

Now the function is less cluttered, more readable, and no longer uses the check for a NULL value.

9. What is the extension method in C#, and how to use them?

Answer:

Interviewers frequently ask about extension methods in C# OOP interviews. This method enables you to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are only in scope when explicitly importing the namespace into your source code using a directive.

10. Can “this” keyword be used within a static method?

Answer:

Since “this” keyword returns a reference to the current instance of a class, we cannot use this inside a static method. And static members exist without any instance of the class and call with the name of the class, not by instance. The “this” keyword is implicitly defined in every constructor and non-static method as a particular kind of reference variable as the first argument of the class type in which it is defined.

以上是C# OOP 面試問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1605
29
PHP教程
1510
276
c#多線程和異步的區別 c#多線程和異步的區別 Apr 03, 2025 pm 02:57 PM

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。

C#與C:歷史,進化和未來前景 C#與C:歷史,進化和未來前景 Apr 19, 2025 am 12:07 AM

C#和C 的歷史與演變各有特色,未來前景也不同。 1.C 由BjarneStroustrup在1983年發明,旨在將面向對象編程引入C語言,其演變歷程包括多次標準化,如C 11引入auto關鍵字和lambda表達式,C 20引入概念和協程,未來將專注於性能和系統級編程。 2.C#由微軟在2000年發布,結合C 和Java的優點,其演變注重簡潔性和生產力,如C#2.0引入泛型,C#5.0引入異步編程,未來將專注於開發者的生產力和雲計算。

c#多線程編程是什麼  c#多線程編程用處 c#多線程編程是什麼 c#多線程編程用處 Apr 03, 2025 pm 02:45 PM

C# 多線程編程是一種讓程序同時執行多項任務的技術,它可以通過提升性能、提高響應能力和實現並行處理來提高程序效率。雖然 Thread 類提供了直接創建線程的方法,但 Task 和 async/await 等高級工具可以提供更安全的異步操作和更簡潔的代碼結構。多線程編程中常見的難題包括死鎖、競態條件和資源洩漏,需要仔細設計線程模型和使用適當的同步機制來避免這些問題。

C#.NET:使用.NET生態系統構建應用程序 C#.NET:使用.NET生態系統構建應用程序 Apr 27, 2025 am 12:12 AM

如何利用.NET構建應用?使用.NET構建應用可以通過以下步驟實現:1)了解.NET基礎知識,包括C#語言和跨平台開發支持;2)學習核心概念,如.NET生態系統的組件和工作原理;3)掌握基本和高級用法,從簡單控制台應用到復雜的WebAPI和數據庫操作;4)熟悉常見錯誤與調試技巧,如配置和數據庫連接問題;5)應用性能優化與最佳實踐,如異步編程和緩存。

從網絡到桌面:C#.NET的多功能性 從網絡到桌面:C#.NET的多功能性 Apr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

c#多線程的好處有哪些 c#多線程的好處有哪些 Apr 03, 2025 pm 02:51 PM

多線程的好處在於能提升性能和資源利用率,尤其適用於處理大量數據或執行耗時操作。它允許同時執行多個任務,提高效率。然而,線程過多會導致性能下降,因此需要根據 CPU 核心數和任務特性謹慎選擇線程數。另外,多線程編程涉及死鎖和競態條件等挑戰,需要使用同步機制解決,需要具備紮實的並發編程知識,權衡利弊並謹慎使用。

.NET框架與C#:解碼術語 .NET框架與C#:解碼術語 Apr 21, 2025 am 12:05 AM

.NETFramework是一個軟件框架,C#是一種編程語言。 1..NETFramework提供庫和服務,支持桌面、Web和移動應用開發。 2.C#設計用於.NETFramework,支持現代編程功能。 3..NETFramework通過CLR管理代碼執行,C#代碼編譯成IL後由CLR運行。 4.使用.NETFramework可快速開發應用,C#提供如LINQ的高級功能。 5.常見錯誤包括類型轉換和異步編程死鎖,調試需用VisualStudio工具。

將C#.NET應用程序部署到Azure/AWS:逐步指南 將C#.NET應用程序部署到Azure/AWS:逐步指南 Apr 23, 2025 am 12:06 AM

如何將C#.NET應用部署到Azure或AWS?答案是使用AzureAppService和AWSElasticBeanstalk。 1.在Azure上,使用AzureAppService和AzurePipelines自動化部署。 2.在AWS上,使用AmazonElasticBeanstalk和AWSLambda實現部署和無服務器計算。

See all articles