.NET面試基礎知識

本文將介紹關於.Net basic的非常有限的概念。

主題

  • Access level of access modifiers
  • Access modifiers for types and type members
  • Default access modifier for class and its members
  • IEnumerable vs. IEnumerator
  • IEnumerable vs. IQueryable
  • Single method interface as IComparable vs. IComparer
  • Variable declared as const vs. readOnly
  • Default value expressions
  • Passing parameters to methods and return value
  • ref return values and ref locals
  • Sealed class and sealed method
  • EF Core development approaches
  • Compile-time vs. run-time polymorphism
  • Abstract method vs. virtual method
  • Abstract class vs. interface
  • Application domain
  • Serialization vs. deserialization
  • Use of stream
  • Http post vs. put
  • Process vs. thread
  • The mechanisms to run code in parallel
  • Synchronization mechanisms in threads

職位和職責

不同公司的職位和工作職責不同。在面試中,工作職責和經驗對這個職位很重要。程序員職位有一年的經驗他們會關注oops概念、並行編程、算法和解決問題的能力等等。另一方面,如果這個職位需要夫妻幾年經驗之後,他們可以專注於最新的框架、語言及其特點,單元測試概念、場景基礎問題,軟件開發方法,設計原則,設計模式,最佳實踐指導方針和軟件架構等。

深入瞭解.net的基本概念

訪問級別的訪問修飾符

Private訪問限制在包含它的類中。在下面的圖中,privateA只能在ClassA中訪問,其他類無法訪問它。

Protected

可以在包含類中訪問,也可以訪問從該類派生的所有類。例如,classA的ProtectedB可以在包含的classA內部訪問,也可以從assembly - a中的派生類ClassB訪問。它也可以從另一個assembly - b中的派生類類類類中訪問。

Note不要僅僅關注訪問修飾符和類之間的關係來判斷不同的類圖。

Internal對同一程序集中聲明的所有類都可訪問。例如,ClassA的InternlC可以在包含類ClassA的內部訪問,也可以被任何類ClassB和class .ses訪問在同一個彙編程序集中。

Private protected (C# 7.2)可由同一程序集中的任何派生類訪問。例如,classA的PrivateProtectedD可以在包含類classA的內部訪問,也可以從彙編- a中的派生類classB訪問。

Protected internal可訪問同一程序集中聲明的所有類或從另一個程序集中的派生類中聲明的所有類。例如,ClassA的ProtectedInternalE可以從同一程序集a中的所有類ClassA、ClassB和ClassC訪問,也可以從另一個程序集b中的派生類類類訪問。

Public

由任何人訪問。例如,可以從程序集a或程序集b中的所有類訪問ClassA的PublicF。

類型和類型成員的訪問修飾符,

類型(類、結構、枚舉、接口、委託等)只能有內部和公共訪問修飾符。

類型成員(字段、屬性、構造函數、方法等)可以擁有所有的訪問修飾符。

類及其成員的默認訪問修飾符

如果沒有指定訪問修飾符,

內部是類的默認值,

成員默認爲private。

IEnumerable vs. IEnumerator

這些都是向前使用的,並且只讀取一個集合的訪問權限。

  • IEnumerable使用IEnumerator,它可以與foreach語句一起使用。
  • IEnumerator有MoveNext、重置方法和當前屬性。它可以與while語句一起使用。
    1. List < string > stringList = new List < string > () {
    2. "Rony",
    3. "Anna",
    4. "Jhon"
    5. };
    6. //// IEnumerator
    7. Console.WriteLine("IEnumerator:n");
    8. IEnumerator < string > enumeratorStringList = stringList.GetEnumerator();
    9. while (enumeratorStringList.MoveNext()) {
    10. Console.WriteLine(enumeratorStringList.Current);
    11. }
    12. //// IEnumerable
    13. Console.WriteLine("nIEnumerable:n");
    14. IEnumerable < string > enumerableStringList = stringList;
    15. foreach(string item in enumerableStringList) {
    16. Console.WriteLine(item);
    17. }

IEnumerable vs. IQueryable

們都可以用於向前數據訪問。

IEnumerable

  • 以從內存集合中查詢數據(比如,列表)
  • 它在內存中加載數據(服務器端到客戶端),同時從數據庫查詢數據,然後過濾客戶端數據。
  • 不支持自定義查詢。
  • 不支持延遲加載。
  • 適合於LINQ-to-Object或LINQ-to-XML。

IQueryable

可以從內存之外查詢數據(服務器端類、遠程數據庫、web服務)

它在數據庫查詢時過濾服務器端的數據,然後發送到客戶端。它提高了性能。

支持自定義查詢(CreateQuery, Execute方法)。

支持延遲加載。

linq to sql

IComparable vs. IComparer

它們都可以用於集合中的自定義排序。主要的區別是 IComparable允許內部排序實現,而IComparer允許外部定製排序實現。

IComparable

IComparer

IComparer using lambda expression,

  1. personObjList.Sort((x, y) => x.Name.CompareTo(y.Name));
  2. personObjList.Sort((x, y) => x.Age.CompareTo(y.Age));

IComparer on multiple properties in the class,

  1. public class PersonComparer: IComparer < Person > {
  2. public enum SortBy {
  3. FirstName,
  4. Age
  5. }
  6. private SortBy sortBy;
  7. public PersonComparer(SortBy sortBy) {
  8. this.sortBy = sortBy;
  9. }
  10. public int Compare(Person x, Person y) {
  11. switch (this.sortBy) {
  12. case SortBy.FirstName:
  13. return x.FirstName.CompareTo(y.FirstName);
  14. case SortBy.Age:
  15. return x.Age.CompareTo(y.Age);
  16. default:
  17. return x.FirstName.CompareTo(y.FirstName);
  18. }
  19. }
  20. }
  21. //// How to use PersonComparer
  22. personObjList.Sort(new PersonComparer(PersonComparer.SortBy.Age));
  23. //// personObjList.Sort(new PersonComparer(PersonComparer.SortBy.FirstName));

變量聲明爲const vs. readOnly

Const

Const是在聲明時初始化的。否則,它將拋出編譯錯誤。它被稱爲編譯時常數。它不能在運行時更改。默認情況下,它是靜態的。ReadOnly

它可以在聲明時初始化,也可以在同一個類的構造函數中多次設置。它被稱爲運行時常數。

Default value expressions

Passing parameters to methods and return value

  • ref uses to pass variable for input and output; It is initialized before passing.
  • in (C# 7.2) uses to pass variable only for input; It is initialized before passing.
  • out uses only for output; It is initialized inside method.

Example

  1. internal class ParameterPassing {
  2. public void ParameterPassingExample(int valueA, ref int inputAndOutputValueB, in int inputOnlyValueC, out int outputOnlyValueD) {
  3. inputAndOutputValueB = valueA + inputAndOutputValueB;
  4. outputOnlyValueD = inputAndOutputValueB + inputOnlyValueC;
  5. }
  6. }
  7. static void Main(string[] args) {
  8. ////******************* Call *********************////
  9. int valueA = 5;
  10. int inputAndOutputValueB = 6;
  11. int inputOnlyValueC = 7;
  12. int outputOnlyValueD;
  13. ParameterPassing parameterPassing = new ParameterPassing();
  14. parameterPassing.ParameterPassingExample(valueA, ref inputAndOutputValueB, in inputOnlyValueC, out outputOnlyValueD);
  15. }

ref returns on method and ref locals

ref return returns the storage location and ref locals can store that in a local variable. But ref readonly locals(C# 7.2) doesn't allow writes to that object instead of read value.

Sealed Class and sealed method

  • sealed class can't be inherited
  • sealed method can't be override in a derived class

Example of sealed class,

Example of sealed method,

EFcore開發方法

實體框架(EF) Core 2.0不支持DB模型(edmx)的可視化設計器或嚮導。EF Core只支持兩種開發方法,

  • Code-First
  • Database-First.

編譯時和運行時多態性

多態性(指一個名稱、多個表單)一個接口和多個實現。

綁定/類型的多態性

綁定是方法調用到方法實現的連接。

Compile-time polymorphism (early-binding/overloading/static binding)

Method overloading

同一類中的方法的相同名稱採用多種實現形式。

Operator overloading

Run-time polymorphism (late-binding/overriding/dynamic-binding)

It is implemented using inheritance and virtual method.

Abstract method vs. virtual method

  • Virtual method has default implementation as well as provides the derived class with an option of overriding it.
  • Abstract method doesn’t provide default implementation and forces the derived class to override the method.

Abstract class vs. interface

  • Accessibility modifier (public/internal etc.) is allowed for abstract class. Interface doesn't allow accessibility modifier.
  • Class can inherit only one abstract class; class can implement more than one interface.
  • Abstract classes can have default implementations for some of its (virtual) members (methods), but the interface class holds only the signature. It can't have implementation for any of its members. But C# 8(not release yet) supports default implantation of the methods in the interface.Example,

Application domain

應用程序域爲安全提供了應用程序的邏輯隔離邊界。同一個應用程序的所有對象都在同一個應用程序域中創建。應用程序域在單個進程中保持程序集的獨立性。

Serialization vs. deserialization

  • Serialization: Transforming object to XML string.
  • Deserialization: Transforming XML string to object.

More details click here,

Use of stream

當數據量太大時,很難同時將整個數據加載到內存中。流用於從大文件中讀取數據。您可以讀取小塊的數據,其中大文件被分解成小塊。

Http post vs. put

  • Post is used to create new entity
  • Put is used to update an existing entity.

Process vs. thread

線程在共享內存空間中運行,而進程在單獨的內存空間中運行。

雙擊Outlook圖標,就可以在操作系統中啓動應用程序,這是一個過程。流程是應用程序的執行實例。

您可以將“auto spelling & grammar check”和“auto check names”看作是Outlook中的主題。線程是進程內的執行路徑。

The mechanisms to run code in parallel

Example of the simple signatures,

Synchronization mechanisms in threads

當多個線程共享資源(共享數據)時,可能會產生問題。生產者-消費者和讀者-作者問題是最常見的例子。爲了避免這個問題,我們需要同步訪問數據。

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