├── Csharp-Essential └── Questions-And-Answers.md ├── OOP-Concepts ├── S.O.L.I.D-fivePrinciples.md └── baseConcepts.md └── README.md /Csharp-Essential/Questions-And-Answers.md: -------------------------------------------------------------------------------- 1 | # Questions and Answers List 💪 2 | 3 | 1. ❓ **Explain the difference between a class and an object.** 4 | 5 | 💁‍ *In short, a class is the definition of an object, and an object is instance of a class. 6 | We can look at the class as a template of the object: it describes all the properties, methods, states and behaviors that the implementing object will have. As mentioned, an object is an instance of a class, and a class does not become an object until it is instantiated. There can be more instances of objects based on the one class, each with different properties.* 7 | 8 | 2. ❓ **Explain the difference between managed and unmanaged code.** 9 | 10 | 💁‍ *MANAGED CODE is a code created by the .NET compiler. It does not depend on the architecture of the target machine because it is executed by the CLR (Common Language Runtime), and not by the operating system itself. CLR and managed code offers developers few benefits, like garbage collection, type checking and exceptions handling.* 11 | 12 | *On the other hand, UMANAGED CODE is directly compiled to native machine code and depends on the architecture of the target machine. It is executed directly by the operating system. In the unmanaged code, the developer has to make sure he is dealing with memory usage and allocation (especially because of memory leaks), type safety and exceptions manually. In .NET, Visual Basic and C# compiler creates managed code. To get unmanaged code, the application has to be written in C or C++.* 13 | 14 | 3. ❓ **Explain the difference between boxing and unboxing. Provide an example.** 15 | 16 | 💁‍ *Boxing is the process of converting a value type to the type object, and unboxing is extracting the value type from the object. While the boxing is implicit, unboxing is explicit.* 17 | 18 | 🤜 *C# Example* 19 | ``` 20 | int i = 13; 21 | object myObject = i; // boxing 22 | i = (int)myObject; // unboxing 23 | ``` 24 | 25 | 4. ❓ **Discuss the difference between constants and read-only variables.** 26 | 27 | 💁‍ *While constants and read-only variable share many similarities, there are some important differences:* 28 | 29 | * *Constants are evaluated at the compile-time, while the read-only variables are evaluated at the runtime.* 30 | * *Constants support only value-type variables, while read-only variables can hold reference type variables.* 31 | * *Constants should be used when the value is not changing during the runtime, and read-only variables are used mostly when their actual value is unknown before the runtime.* 32 | 33 | 5. ❓ **Explain what LINQ is.** 34 | 35 | 💁‍ *LINQ is an acronym for Language Integrated Query, and was introduced with Visual Studio 2008. LINQ is a set of features that extends query capabilities to the .NET language syntax by adding sets of new standard query operators that allow data manipulation, regardless of the data source. Supported data sources are: .NET Framework collections, SQL Server databases, ADO.NET Datasets, XML documents, and any collection of objectsthat support `IEnumerable` or the generic `IEnumerable` interface, in both C# and Visual Basic. In short, LINQ bridges the gap between the world of objects and the world of data.* 36 | 37 | 6. ❓ **Discuss what garbage collection is and how it works. Provide a code example of how you can enforce garbage collection in .NET.** 38 | 39 | 💁‍ *Garbage collection is a low-priority process that serves as an automatic memory manager which manages the allocation and release of memory for the applications. Each time a new object is created, the common language runtime allocates memory for that object from the managed Heap. As long as free memory space is available in the managed Heap,the runtime continues to allocate space for new objects. However, memory is not infinite, and once an application fills the Heap memory space, garbage collection comes into play to free some memory. When the garbage collector performs a collection, it checks for objects in the managed Heap that are no longer being used by the application and performs the necessary operations to reclaim the memory. Garbage collection will stop all running threads, it will find all objects in the Heap that are not being accessed by the main program and delete them. It will then reorganize all the objects left in the Heap to make space and adjust all the Pointers to these objects in both the Stack and the Heap.* 40 | 41 | 🤜 *To enforce garbage collection in your code manually, you can run the following command (C#)* 42 | ``` 43 | System.GC.Collect(); 44 | ``` 45 | 46 | 7. ❓ **What do the following acronyms in .NET stand for: IL, CIL, MSIL, CLI and JIT?** 47 | 48 | 💁‍ ***IL**, or Intermediate Language, is a CPU independent partially compiled code. IL code will be compiled to native machine code using current environmental properties by **Just-In-Time** compiler (JIT). JIT compiler translates the IL code to an assembly code and uses the CPU architecture of the target machine to execute a .NET application. In .NET, IL is called **Common Intermediate Language** (CIL), and in the early .NET days it was called **Microsoft Intermediate Language** (MSIL).* 49 | 50 | ***CLI**, or Common Language Infrastructure, is an open specification developed by Microsoft. It is a compiled code library used for deployment, versioning, and security. In .NET there are two CLI types: process assemblies (EXE) and library assemblies (DLL). CLI assemblies contain code in CIL, and as mentioned, during compilation of CLI programming languages, the source code is translated into CIL code rather than into platform or processor specific object code.* 51 | 52 | *To summary:* 53 | 54 | 1. *When compiled, source code is first translated to IL (in .NET, that is CIL, and previously called MSIL).* 55 | 2. *CIL is then assembled into a bytecode and a CLI assembly is created.* 56 | 3. *Before code execution, CLI code is passed through the runtime’s JIT compiler to generate native machine code.* 57 | 4. *The computer’s processor executes the native machine code.* 58 | 59 | 8. ❓ **Explain the difference between the Stack and the Heap.** 60 | 61 | 💁‍ *The short answer would be: in the Stack are stored value types (types inherited from `System.ValueType`), and in the Heap are stored reference types (types inherited from `System.Object`).* 62 | 63 | *We can say the Stack is responsible for keeping track of what is actually executing and where each executing thread is (each thread has its own Stack). The Heap, on the other hand, is responsible for keeping track of the data, or more precise objects.* 64 | 65 | 9. ❓ **Explain what inheritance is, and why it’s important.** 66 | 67 | 💁‍ *Inheritance is one of the most important concepts in object-oriented programming, together with encapsulation and polymorphism. Inheritance allows developers to create new classes that reuse, extend, and modify the behavior defined in other classes. This enables code reuse and speeds up development. With inheritance, developers can write and debug one class only once, and then reuse that same code as the basis for the new classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. By default, all classes in .NET are inheritable.* 68 | 69 | 10. ❓ **Explain the differences between an Interface and an Abstract Class in .NET.** 70 | 71 | 💁‍ *An **interface** merely declares a contract or a behavior that implementing classes should have. It may declare only properties, methods, and events with no access modifiers. All the declared members must be implemented.* 72 | 73 | *An **abstract** class provides a partial implementation for a functionality and some abstract/virtual members that must be implemented by the inheriting entities. It can declare fields too.* 74 | 75 | *Neither interfaces nor abstract classes can be instantiated.* 76 | 77 | # References 78 | 79 | 1. [13 Essential .NET Interview Questions*](https://www.toptal.com/dot-net/interview-questions) 80 | 2. [Hire the Best Developer with These 15 .NET Interview Questions](https://www.roberthalf.com/technology/blog/9-net-interview-questions-with-sample-answers) 81 | 3. [50 C# Interview Questions And Answers](http://www.c-sharpcorner.com/uploadfile/8ef97c/c-sharp-net-interview-questions-and-answers) 82 | 83 | -------------------------------------------------------------------------------- /OOP-Concepts/S.O.L.I.D-fivePrinciples.md: -------------------------------------------------------------------------------- 1 | # S.O.L.I.D: The first 5 principles of Object Oriented Design 2 | 3 | ## Introduction 4 | 5 | S.O.L.I.D is an acronym for the first five object-oriented design(OOD) principles by Robert C. Martin, popularly known as **Uncle Bob**. 6 | 7 | These principles, when combined together, make it easy for a programmer to develop software that are easy to maintain and extend. They also make it easy for developers to avoid code smells, easily refactor code, and are also a part of the agile or adaptive software development. 8 | 9 | ## S.O.L.I.D STANDS FOR 10 | 11 | When expanded the acronyms might seem complicated, but they are pretty simple to grasp. 12 | 13 | * S – Single-responsiblity principle 14 | * O – Open-closed principle 15 | * L – Liskov substitution principle 16 | * I – Interface segregation principle 17 | * D – Dependency Inversion Principle 18 | 19 | ## Details 20 | 21 | 1. Single-responsibility Principle 22 | 23 | 24 | ## References 25 | 26 | 1. https://scotch.io/bar-talk/s-o-l-i-d-the-first-five-principles-of-object-oriented-design (code in PHP) 27 | -------------------------------------------------------------------------------- /OOP-Concepts/baseConcepts.md: -------------------------------------------------------------------------------- 1 | # OOP Concepts 2 | 3 | ## Câu hỏi 4 | 5 | 1. Lập trình hướng đối tượng (OOP) có những tính chất đặc thù gì? Nêu chi tiết. 6 | 2. Viết chương trình minh hoạ cho các tính chất trên. 7 | 8 | ## Giải quyết 9 | 10 | 1. OOP có 04 tính chất đặc thù 11 | * **Tính đóng gói (Encapsulation)**: 12 | 13 | Có thể gói dữ liệu (data, ~ biến, trạng thái) và mã chương trình (code, ~ phương thức) thành một cục gọi là lớp (class) để dễ quản lí. Trong cục này thường data rất rối rắm, không tiện cho người không có trách nhiệm truy cập trực tiếp, nên thường ta sẽ che dấu data đi, chỉ để lòi phương thức ra ngoài. Ví dụ hàng xóm sang mượn búa, thay vì bảo hàng xóm cứ tự nhiên vào lục lọi, ta sẽ bảo: "Ấy bác ngồi chơi để tôi bảo cháu lấy cho". Ngôn ngữ Ruby "phát xít" đến nỗi dấu tiệt data, cấm không cho truy cập từ bên ngoài. Ngoài ra, các lớp liên quan đến nhau có thể được gom chung lại thành package (tùy ngôn ngữ mà còn gọi là module, namespace v.v.). 14 | 15 | * **Tính trừu tượng (Abstraction)** 16 | 17 | Có câu "program to interfaces, not to concrete implementations". Nghĩa là khi viết chương trình theo phong cách hướng đối tượng, khi thiết kế các đối tượng, ta cần rút tỉa ra những đặc trưng của chúng, rồi trừu tượng hóa thành các interface, và thiết kế xem chúng sẽ tương tác với nhau như thế nào. Nói cách khác, chúng ta định ra các interface và các contract mà chúng cần thỏa mãn. 18 | 19 | * **Tính thừa kế (Inheritance)** 20 | 21 | Lớp cha có thể chia sẻ dữ liệu và phương thức cho các lớp con, các lớp con khỏi phải định nghĩa lại những logic chung, giúp chương trình ngắn gọn. Nếu lớp cha là interface, thì lớp con sẽ di truyền những contract trừu tượng từ lớp cha. 22 | 23 | * **Tính đa hình (Polymorphism)** 24 | 25 | Đối tượng có thể thay đổi kiểu (biến hình). (1) Với các ngôn ngữ OOP có kiểu, có thể mượn phát biểu của C++ "con trỏ kiểu lớp cha có thể dùng để trỏ đến đối tượng kiểu lớp con". Như vậy khi khai báo chỉ cần khai báo p có kiểu lớp cha, còn sau đó nó trỏ đến đâu thì kệ cha con nó: nếu cha và con cùng có phương thức m, thì từ p cứ lôi m ra gọi thì chắc chắn gọi được, không cần biết hiện tại p đang trỏ đến cha hay con. Khi lớp B thừa kế từ lớp A, thì đối tượng của lớp B có thể coi là đối tượng của lớp A, vì B chứa nhiều thứ thừa kế từ A. (2) Với ngôn ngữ OOP không có kiểu như Ruby, có thể mượn phát biểu của phương pháp xác định kiểu kiểu con vịt: "nếu p đi như vịt nói như vịt, thì cứ coi nó là vịt". Như vậy nếu lớp C có phương thức m, mà có thể gọi phương thức m từ đối tượng p bất kì nào đó, thì cứ coi p có kiểu là C. 26 | 27 | 2. Chương trình chứng minh 28 | 29 | * Tạo interface Animal có phương thức say_hello. <- Thể hiện tính trừu tượng, có nghĩa ta định ra contract là rằng dù là con vật gì đi nữa thì nó cũng có phương thức say_hello để chào hỏi gì đấy. 30 | * Tạo 2 lớp Cat và Dog kế thừa từ Animal. Khi khởi tạo chúng sẽ có tên. Chúng override lại phương thức say_hello để chào hỏi theo cách riêng của chúng. <- Thể hiện tính đóng gói (đóng gói biến tên và phương thức say_hello với nhau) và tính thừa kế (Cat và Dog mang đặc điểm chung là có say_hello từ Animal). 31 | * Tạo lớp Zoo để quản lí nhiều Animal, có (1) phương thức add, remove để thêm, bớt các Animal (các đối tượng của các lớp thừa kế từ Animal), (2) phương thức say_hello_all để gọi say_hello của tất cả đối tượng nó quản lí. <- Thể hiện tính đa hình, Zoo gọi chỉ gọi một phương thức say_hello, nhưng tùy con vật mà lời chào hỏi sẽ khác nhau. 32 | 33 | ## References 34 | 35 | 1. https://kipalog.com/posts/4-tinh-chat-dac-thu-cua-lap-trinh-huong-doi-tuong 36 | 2. https://chesterli0130.wordpress.com/2012/10/04/four-major-principles-of-object-oriented-programming-oop/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OOP and .NET with CSharp 2 | 3 | ## Tools 4 | 5 | 1. https://dotnetfiddle.net/ - .NET Online code editor 6 | 7 | ## Knowdledges 8 | 9 | 1. [The principles of Object Oriented Programming](https://github.com/quangnd/OOP-And-CSharp/blob/master/OOP-Concepts/baseConcepts.md) 10 | 2. [The essential interview questions by C#](https://github.com/quangnd/OOP-And-CSharp/blob/master/Csharp-Essential/Questions-And-Answers.md) 11 | 3. Examples were written in C# 12 | --------------------------------------------------------------------------------