C# 1001 notes
6.51K subscribers
329 photos
9 videos
2 files
313 links
Регулярные короткие заметки по C# и .NET.

Просто о сложном для каждого.

admin - @haarrp
加入频道
Which of the following statements is not valid to create new object in C#?
Anonymous Quiz
11%
var a = new Int32();
6%
var a = new String();
58%
var a = new IComparable();
25%
var a = new [] {0};
​​Explain what inheritance is, and why it’s important

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.
📝 What interface should your data structure implement to make the Where method work?

Implementing IEnumerable makes using foreach and where possible.

🔸 IEnumerable interface contains the System.Collections.Generic namespace.

🔸 IEnumerable interface is a generic interface which allows looping over generic or non-generic lists.

🔸 IEnumerable interface also works with linq query expression.

🔸 IEnumerable interface Returns an enumerator that iterates through the collection.
If you run C# executable file multiple times, multiple processes are created. If you want to have only one application process even if you launch multiple times, what can you use?
Anonymous Quiz
22%
Semaphore
41%
Mutex
10%
Critical Section
26%
C# lock
​​What do the following acronyms in .NET stand for: IL, CIL, MSIL, CLI and JIT?

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).

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.

To summary:
- When compiled, source code is first translated to IL (in .NET, that is CIL, and previously called MSIL).
- CIL is then assembled into a bytecode and a CLI assembly is created.
- Before code execution, CLI code is passed through the runtime’s JIT compiler to generate native machine code.
- The computer’s processor executes the native machine code.
📝 What is difference between late binding and early binding in C#?

🔸 Early Binding or Static Binding. The name itself describes that compiler knows about what kind of object it is, what are all the methods and properties it contains. As soon as you declared the object, .NET Intellisense will populate its methods and properties on click of the dot button.

🔸 Late Binding or Dynamic Binding. The name itself describes that compiler does not know what kind of object it is, what are all the methods and properties it contains. You have to declare it as an object, later you need get the type of the object, methods that are stored in it. Everything will be known at the run time.
Which of the following operators cannot use operator overloading?
Anonymous Quiz
13%
operator ++
11%
operator &
10%
operator ||
65%
operator true
​​Discuss what garbage collection is and how it works. Provide a code example of how you can enforce garbage collection in .NET

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.

To enforce garbage collection in your code manually, you can run the following command (C#):

System.GC.Collect();
📝 Is operator overloading supported in C#?

A user-defined type can overload a predefined C# operator. That is, a type can provide the custom implementation of an operation in case one or both of the operands are of that type.

The above function implements the addition operator (+) for a user-defined class Box. It adds the attributes of two Box objects and returns the resultant Box object.
In multithread programming, which of the followings is not using Thread Pool?
Anonymous Quiz
18%
BackgroundWorker class
26%
Asynchronous delegate
35%
Thread class
21%
Task class
​​Explain what LINQ is

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 objects that support IEnumerable or the generic IEnumerable<T> interface, in both C# and Visual Basic. In short, LINQ bridges the gap between the world of objects and the world of data.
📝 Why to use lock statement in C#?

The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter a locked code, it will wait, block, until the object is released.

The lock keyword calls Enter at the start of the block and Exit at the end of the block. lock keyword actually handles Monitor class at back end.
Class A has [Serializable()] attribute. When is [Serializable] checked?
Anonymous Quiz
29%
At C# compile time
41%
At CLR runtime
19%
At JIT compile time
11%
At Linking
​​Discuss the difference between constants and read-only variables

While constants and read-only variable share many similarities, there are some important differences:

- Constants are evaluated at the compile-time, while the read-only variables are evaluated at the runtime.

- Constants support only value-type variables, while read-only variables can hold reference type variables.

- 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.
📝 What is the Constructor Chaining in C#?

Constructor Chaining is an approach where a constructor calls another constructor in the same or base class. This is very handy when we have a class that defines multiple constructors.
​​Explain the difference between managed and unmanaged code

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.

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++.
📝 What is the difference between dispose and finalize methods in C#?

Finalizer and Dispose both are used for same task like to free unmanaged resources but have some differences see.

Finalize:

🔸 Finalize used to free unmanaged resources those are not in use like files, database connections in application domain and more, held by an object before that object is destroyed.

🔸 In the Internal process it is called by Garbage Collector and can't called manual by user code or any service.

🔸 Finalize belongs to System.Object class.

🔸 Implement it when you have unmanaged resources in your code, and make sure that these resources are freed when the Garbage collection happens.

Dispose:

🔸 Dispose is also used to free unmanaged resources those are not in use like files, database connections in Application domain at any time.

🔸 Dispose explicitly it is called by manual user code.

🔸 If we need to dispose method so must implement that class by IDisposable interface.