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

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

admin - @haarrp
加入频道
What is the result of variable a and b?
Anonymous Quiz
22%
a=true, b=true
34%
a=true, b=false
19%
a=false, b=true
26%
a=false, b=false
What are the fundamental concepts of OOP?

OOP stands for Object-Oriented Programming and the following are the fundamental concepts:

- Encapsulation: It is the internal representation of an object which is hidden from the outside view of an object’s definition. Only required information is accessed while the other data are hidden.

- Abstraction: It is defined as the process of identifying critical behavior, eliminating the irrelevant details and data of an object.

- Inheritance: It is defined as the process of creating new classes from another class. This can be achieved by accessing, modifying, and extending the behavior of the objects in their parent class.

- Polymorphism: It is a term used for describing one name in many forms. It can be achieved through the same name but different implementations.

https://youtu.be/m_MQYyJpIjg
📝 What is the difference between is and as operators in C#?

The is operator checks if an object can be cast to a specific type.

 if (someObject is StringBuilder) ...

The as operator attempts to cast an object to a specific type, and returns null if it fails.

StringBuilder b = someObject as StringBuilder;
if (b != null) ...
State the difference between is and as operators in C#

The difference between is and as an operator in C# is that is the operator is used for checking the compatibility of an object for a given type and returning to the Boolean for the results.Whereas as the operator is used for casting of an object to a type or to a class.

https://youtu.be/IKmRtJcRX_I
📝 Can you create a function in C# which can accept varying number of arguments?

By using the params keyword, you can specify a method parameter that takes a variable number of arguments. No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration. The declared type of the params parameter must be a single-dimensional array.
​​State the difference between the “throw” and “throw ex” in .NET

The difference between “throw” and “throw ex” is that “throw” is used for preserving original error stack whereas “throw ex” has a throw point through which it can trace the stack. For more accurate, error-free information, the throw is advisable to use.
📝 What is the best practice to have best performance using Lazy objects?

You typically use it when you want to instantiate something the first time its actually used. This delays the cost of creating it till if/when it's needed instead of always incurring the cost.

Usually this is preferable when the object may or may not be used and the cost of constructing it is non-trivial.

For example Lazy<T> makes implementing lazy, thread-safe singletons easy.
C# / .NET supports various built-in data structures. Which of the following data structures does not exist as built-in?
Anonymous Quiz
2%
Array
45%
D-Array
37%
Binary Tree
5%
Stack
11%
Linked List
​​What are the different types of serialization?

Following are the three different types of serialization:

- XML serialization: All the public properties are serialized to the XML document.

- SOAP: Any system that understands SOAP can use this serialization. System.Runtime.Serialization is the place where all the classes reside.

- Binary serialization: It is used for the conversion of any code into binary form.
📝 What is scope of a Protected Internal member variable of a C# class?

The protected internal access specifier allows a class to hide its member variables and member functions from other class objects and functions, except a child class within the same application. This is also used while implementing inheritance.
​​What does thread pooling mean?

The collection of threads is known as thread pooling. These threads find application when the tasks are to be performed without disturbing the primary threads. To manage the operations of the threads in the pool, System.Threading.ThreadPool namespace is used.
In C#, what is similar to C++ function pointer?
Anonymous Quiz
6%
Event
6%
Interface
77%
Delegate
11%
Method
​​Asynchronous programming with async, await, Task in C# (part 1)

C# and .NET Framework (4.5 & Core) supports asynchronous programming using some native functions, classes, and reserved keywords.

Before we see what is asynchronous programming, let's understand what is synchronous programming using the following console example.

In the above example, the LongProcess() method is some long-running task such as reading a file from the server, calling a web API that returns a large amount of data or uploading or downloading a big file. It takes a little longer time to execute (Thread.Sleep(4000) holds it for 4 seconds just to show long execution time). The ShortProcess() is a simple method that gets executed after the LongProcess() method.

The above program executes synchronously. It means execution starts from the Main() method wherein it first executes the LongProcess() method and then ShortProcess() method. During the execution, an application gets blocked and becomes unresponsive (You can see this in Windows-based applications mainly). This is called synchronous programming where execution does not go to next line until the current line executed completely.
​​What is Asynchronous Programming? (part 2)

In asynchronous programming, the code gets executed in a thread without having to wait for an I/O-bound or long-running task to finish. For example, in the asynchronous programming model, the LongProcess() method will be executed in a separate thread from the thread pool, and the main application thread will continue to execute the next statement.

Microsoft recommends Task-based Asynchronous Pattern  to implement asynchronous programming in the .NET Framework or .NET Core applications using async , await keywords and Task or Task<TResult> class.

Now let's rewrite the above example in asynchronous pattern using async keyword.

In the above example, the Main() method is marked by the async keyword, and the return type is Task. The async keyword marks the method as asynchronous. Note that all the methods in the method chain must be async in order to implement asynchronous programming. So, the Main() method must be async to make child methods asynchronous.

The LongProcess() method is also marked with the async keyword which makes it asynchronous. The await Task.Delay(4000); holds the thread execute for 4 seconds.

Now, the program starts executing from the async Main() method in the main application thread. The async LongProcess() method gets executed in a separate thread and the main application thread continues execution of the next statement which calls ShortProcess() method and does not wait for the LongProcess() to complete.
​​async, await, and Task (part 3)

Use async along with await and Task if the async method returns a value back to the calling code. We used only the async keyword in the above program to demonstrate the simple asynchronous void method.

The await keyword waits for the async method until it returns a value. So the main application thread stops there until it receives a return value.

The Task class represents an asynchronous operation and Task<TResult> generic class represents an operation that can return a value. In the above example, we used await Task.Delay(4000) that started async operation that sleeps for 4 seconds and await holds a thread until 4 seconds.

The following demonstrates the async method that returns a value.

In the above example, in the static async Task<int> LongProcess() method, Task<int> is used to indicate the return value type int. int val = await result; will stop the main thread there until it gets the return value populated in the result. Once get the value in the result variable, it then automatically assigns an integer to val.

An async method should return voidTask, or Task<TResult>, where TResult is the return type of the async method. Returning void is normally used for event handlers. The async keyword allows us to use the await keyword within the method so that we can wait for the asynchronous method to complete for other methods which are dependent on the return value.