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

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

admin - @haarrp
加入频道
#challenge

💻 Oddish vs. Evenish | #easy

Create a function that determines whether a number is Oddish or Evenish. A number is Oddish if the sum of all of its digits is odd, and a number is Evenish if the sum of all of its digits is even. If a number is Oddish, return "Oddish". Otherwise, return "Evenish".

For example, OddishOrEvenish(121) should return "Evenish", since 1 + 2 + 1 = 4. OddishOrEvenish(41) should return "Oddish", since 4 + 1 = 5.

Examples:

OddishOrEvenish(43) ➞ "Oddish"
// 4 + 3 = 7
// 7 % 2 = 1

OddishOrEvenish(373) ➞ "Oddish"
// 3 + 7 + 3 = 13
// 13 % 2 = 1

OddishOrEvenish(4433) ➞ "Evenish"
// 4 + 4 + 3 + 3 = 14
// 14 % 2 = 0

🏆 Leave your solutions in the comments. The solution will be posted below in a couple of hours 👇

#interview
Here is a solution for the #challenge above
📝 What is Boxing and Unboxing?

Boxing and Unboxing both are used for type conversion but have some difference:

🔸 Boxing is the process of converting a value type data type to the object or to any interface data type which is implemented by this value type. When the CLR boxes a value means when CLR is converting a value type to Object Type, it wraps the value inside a System.Object and stores it on the heap area in application domain.

🔸 Unboxing is also a process which is used to extract the value type from the object or any implemented interface type. Boxing may be done implicitly, but unboxing have to be explicit by code.

The concept of boxing and unboxing underlines the C# unified view of the type system in which a value of any type can be treated as an object.

#post
What will be the output of the following code snippet?
Anonymous Quiz
52%
0 1 2 3
6%
1 2 3 4
30%
4 4 4 4
13%
Error
#challenge

💻 Check for Anagrams | #easy

Create a function that takes two strings and returns either true or false depending on whether they're anagrams or not.

Examples:

IsAnagram("cristian", "Cristina") ➞ true
IsAnagram("Dave Barry", "Ray Adverb") ➞ true
IsAnagram("Nope", "Note") ➞ false

🏆 Leave your solutions in the comments. The solution will be posted below in a couple of hours 👇

#interview
Here is a solution for the #challenge above
📝 What is the difference between string and StringBuilder?

String:

🔸 It's an immutable object that hold string value.

🔸 Performance wise string is slow because its' create a new instance to override or change the previous value.

🔸 String belongs to System namespace.

StringBuilder:

🔸 StringBuilder is a mutable object.

🔸 Performance wise StringBuilder is very fast because it will use same instance of StringBuilder object to perform any operation like insert value in existing string.

🔸 StringBuilder belongs to System.Text.Stringbuilder namespace.

#post
What will be the output of the following code snippet?
Anonymous Quiz
35%
20, 40, 35, 85, 70
12%
20, 40, 35, 85, 70, 60
49%
20, 35, 40, 60, 70, 85
3%
20, 35, 40, 60, 70
What will be the output of the following code snippet?
Anonymous Quiz
18%
1 0
32%
1 1
25%
0 1
25%
0 0
What are Reference Types in C#?

The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables.

In other words, they refer to a memory location. Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value. Example of built-in reference types are: object, dynamic, and string.
What will be the output of the following code snippet?
Anonymous Quiz
16%
4, 11
76%
1, 11
2%
4, 7
6%
1, 7
What is the volatile keyword used for?

In C# volatile tells the compiler that the value of a variable must never be cached as its value may change outside of the scope of the program itself (such as the operating system, the hardware, or a concurrently executing thread). The compiler will then avoid any optimisations that may result in problems if the variable changes "outside of its control".

Or in more simple terms:

Sometimes, the compiler will optimize a field and use a register to store it. If thread 1 does a write to the field and another thread accesses it, since the update was stored in a register (and not memory), the 2nd thread would get stale data.

You can think of the volatile keyword as saying to the compiler "I want you to store this value in memory". This guarantees that the 2nd thread retrieves the latest value.
What is Managed or Unmanaged Code?

🔸 Managed Code - The code, which is developed in .NET framework is known as managed code. This code is directly executed by CLR with the help of managed code execution. Any language that is written in .NET Framework is managed code.

🔸 Unmanaged Code - The code, which is developed outside .NET framework is known as unmanaged code. Applications that do not run under the control of the CLR are said to be unmanaged, and certain languages such as C++ can be used to write such applications, which, for example, access low - level functions of the operating system. Background compatibility with the code of VB, ASP and COM are examples of unmanaged code.
What is the use of static constructors?

A static constructor is useful for initializing any static fields associated with a type (or any other per-type operations) - useful in particular for reading required configuration data into readonly fields, etc.

It is run automatically by the runtime the first time it is needed (the exact rules there are complicated (see "beforefieldinit"), and changed subtly between CLR2 and CLR4). Unless you abuse reflection, it is guaranteed to run at most once (even if two threads arrive at the same time).

You can't overload it.