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

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

admin - @haarrp
加入频道
What is enum in C#?

An enum is a value type with a set of related named constants often referred to as an enumerator list. The enum keyword is used to declare an enumeration. It is a primitive data type, which is user defined. An enum is used to create numeric constants in .NET framework. All the members of enum are of enum type. Their must be a numeric value for each enum type.

Some points about enum:

🔸 Enums are enumerated data type in C#.

🔸 Enums are strongly typed constant. They are strongly typed, i.e. an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same.

🔸 Enumerations (enums) make your code much more readable and understandable.

🔸 Enum values are fixed. Enum can be displayed as a string and processed as an integer.

🔸 The default type is int, and the approved types are byte, sbyte, short, ushort, uint, long, and ulong.

🔸 Enums are value types and are created on the stack and not on the heap.
What will be the output of the following code snippet?
Anonymous Quiz
59%
Compile error
20%
-2
9%
2
12%
0
List some different ways for equality check in .NET

🔸 The ReferenceEquals() method - checks if two reference type variables(classes, not structs) are referred to the same memory address.

🔸 The virtual Equals() method. (System.Object) - checks if two objects are equivalent.

🔸 The static Equals() method - is used to handle problems when there is a null value in the check.

🔸 The Equals method from IEquatable interface.

🔸 The comparison operator == - usually means the same as ReferenceEquals, it checks if two variables point to the same memory address. The gotcha is that this operator can be override to perform other types of checks. In strings, for instance, it checks if two different instances are equivalent.
In how many ways you can pass parameters to a method?

There are three ways that parameters can be passed to a method:

🔸 Value parameters − This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.

🔸 Reference parameters − This method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.

🔸 Output parameters − This method helps in returning more than one value.
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.