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

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

admin - @haarrp
加入频道
This media is not supported in your browser
VIEW IN TELEGRAM
📝 What are the benefits of a Deferred Execution in LINQ?

In LINQ, queries have two different behaviors of execution: immediate and deferred.

Deferred execution means that the evaluation of an expression is delayed until its realized value is actually required. It greatly improves performance by avoiding unnecessary execution.
Consider:

 results = collection
.Select(item => item.Foo)
.Where(foo => foo < 3)
.ToList();

With deferred execution, the above iterates your collection one time, and each time an item is requested during the iteration, performs the map operation, filters, then uses the results to build the list.

If you were to make LINQ fully execute each time, each operation (Select / Where) would have to iterate through the entire sequence. This would make chained operations very inefficient.

#post
#challenge

💻 Pentagonal Number | #easy

Write a function that takes a positive integer num and calculates how many dots exist in a pentagonal shape around the center dot on the Nth iteration.

In the image below you can see the first iteration is only a single dot. On the second, there are 6 dots. On the third, there are 16 dots, and on the fourth there are 31 dots.

Return the number of dots that exist in the whole pentagon on the Nth iteration.

Examples:

pentagonal(1) ➞ 1
pentagonal(2) ➞ 6
pentagonal(3) ➞ 16
pentagonal(8) ➞ 141

🏆 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 are the different types of classes in C#?

The
different types of class in C# are:

🔸 Partial class – Allows its members to be divided or shared with multiple .cs files. It is denoted by the keyword Partial.

🔸 Sealed class – It is a class which cannot be inherited. To access the members of a sealed class, we need to create the object of the class. It is denoted by the keyword Sealed.

🔸 Abstract class – It is a class whose object cannot be instantiated. The class can only be inherited. It should contain at least one method. It is denoted by the keyword abstract.

🔸 Static class – It is a class which does not allow inheritance. The members of the class are also static. It is denoted by the keyword static. This keyword tells the compiler to check for any accidental instances of the static class.

#post
What will be the output of the following code snippet?
Anonymous Quiz
49%
abc def ghi
27%
ghi ghi ghi
5%
abc abc abc
19%
Error
📝 What is Weak Reference in C#?

The garbage collector cannot collect an object in use by an application while the application's code can reach that object. The application is said to have a strong reference to the object.

A weak reference permits the garbage collector to collect the object while still allowing the application to access the object. A weak reference is valid only during the indeterminate amount of time until the object is collected when no strong references exist.

Weak references are useful for objects that use a lot of memory, but can be recreated easily if they are reclaimed by garbage collection.

Suppose a tree view in a Windows Forms application displays a complex hierarchical choice of options to the user. If the underlying data is large, keeping the tree in memory is inefficient when the user is involved with something else in the application.

When the user switches away to another part of the application, you can use the WeakReference class to create a weak reference to the tree and destroy all strong references. When the user switches back to the tree, the application attempts to obtain a strong reference to the tree and, if successful, avoids reconstructing the tree.

#post
#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