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

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

admin - @haarrp
加入频道
What will be the output of the following code snippet?
Anonymous Quiz
8%
Runtime Error
15%
Compile time Error
64%
Division By Zero
14%
Infinity
Explain type in C#

Anonymous types allow us to create a new type without defining them. This is way to defining read only properties into a single object without having to define type explicitly. Here Type is generating by the compiler and it is accessible only for the current block of code. The type of properties is also inferred by the compiler.
What will be the output of the following code snippet?
Anonymous Quiz
75%
0 1 2 3 4 5 6 7 8 9
11%
32333435363738394041
14%
Compile time Error
Explain Code Compilation in C#

There are four steps in code compilation which include:

🔸 Compiling the source code into Managed code by C# compiler.

🔸 Combining the newly created code into assemblies.

🔸 Loading the Common Language Runtime(CLR).

🔸 Executing the assembly by CLR.
What will be the output of the following code snippet?
Anonymous Quiz
32%
True
59%
False
7%
Compilation error
2%
Error
What is scope of a Internal member variable of a C# class?

Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly. In other words, any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined.

You can use it for utility or helper classes/methods that you would like to access from many other classes within the same assembly, but that you want to ensure code in other assemblies can't access.
I've noticed there were only 7% of correct answers (🎉) on the last tricky question (I was also confused 🤡)

Let me explain why it happens.

C# specification: https://www.ecma-international.org/wp-content/uploads/ECMA-334_5th_edition_december_2017.pdf
📝 What is deep or shallow copy concept in C#?

🔸 Shallow Copy is about copying an object's value type fields into the target object and the object's reference types are copied as references into the target object but not the referenced object itself. It copies the types bit by bit. The result is that both instances are cloned and the original will refer to the same object.

🔸 Deep Copy is used to make a complete deep copy of the internal reference types, for this we need to configure the object returned by MemberwiseClone().

In other words a deep copy occurs when an object is copied along with the objects to which it refers.

#post
📝 What is Extension Method in C# and how to use them?

Extension methods allow you to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. An extension method is a special kind of static method, but they are called as if they were instance methods on the extended type.
​​📝 What is sealed Class in C#?

When
applied to a class, the sealed modifier prevents other classes from inheriting from it. In the following example, class B inherits from class A, but no class can inherit from class B.

You can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties.

Summary:

🔸 Once a class is defined as a sealed class, the class cannot be inherited.

🔸 Structs are also sealed.
📝 Is there a way to catch multiple exceptions at once and without code duplication?

Consider this code.

Is there a way to catch both exceptions and only call the WebId = Guid.Empty call once?

The answer will be post soon.
The answer to the previous question is: yes, you can catch System.Exception and switch on the exception types.
📝 Why to use of the IDisposable interface?

The "primary" use of the IDisposable interface is to clean up unmanaged resources. Note the purpose of the Dispose pattern is to provide a mechanism to clean up both managed and unmanaged resources and when that occurs depends on how the Dispose method is being called.
📝 Explain the difference between Task and Thread in .NET

🔸 Thread represents an actual OS-level thread, with its own stack and kernel resources. Thread allows the highest degree of control; you can Abort() or Suspend() or Resume() a thread, you can observe its state, and you can set thread-level properties like the stack size, apartment state, or culture. ThreadPool is a wrapper around a pool of threads maintained by the CLR.

🔸 The Task class from the Task Parallel Library offers the best of both worlds. Like the ThreadPool, a task does not create its own OS thread. Instead, tasks are executed by a TaskScheduler; the default scheduler simply runs on the ThreadPool. Unlike the ThreadPool, Task also allows you to find out when it finishes, and (via the generic Task) to return a result.
​​Explain the differences between an Interface and an Abstract Class in .NET?

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.

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.

Neither interfaces nor abstract classes can be instantiated.