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

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

admin - @haarrp
加入频道
​​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.
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.