📝 When to use Finalize vs Dispose?
🔸 The finalizer method is called when your object is garbage collected and you have no guarantee when this will happen (you can force it, but it will hurt performance).
🔸 The Dispose method, on the other hand, is meant to be called by the code that created your class so that you can clean up and release any resources you have acquired (unmanaged data, database connections, file handles, etc) the moment the code is done with your object.
The standard practice is to implement
And in your finalizer, you call
#post
🔸 The finalizer method is called when your object is garbage collected and you have no guarantee when this will happen (you can force it, but it will hurt performance).
🔸 The Dispose method, on the other hand, is meant to be called by the code that created your class so that you can clean up and release any resources you have acquired (unmanaged data, database connections, file handles, etc) the moment the code is done with your object.
The standard practice is to implement
IDisposable
and Dispose
so that you can use your object in a using
statement such as using(var foo = new MyObject()) { }
.And in your finalizer, you call
Dispose
, just in case the calling code forgot to dispose of you.#post
#challenge
💻 Perfect Number | #easy
Create a function that tests whether or not an integer is a perfect number. A perfect number is a number that can be written as the sum of its factors, (equal to sum of its proper divisors) excluding the number itself.
For example, 6 is a perfect number, since 1 + 2 + 3 = 6, where 1, 2, and 3 are all factors of 6. Similarly, 28 is a perfect number, since 1 + 2 + 4 + 7 + 14 = 28.
Examples:
#interview
💻 Perfect Number | #easy
Create a function that tests whether or not an integer is a perfect number. A perfect number is a number that can be written as the sum of its factors, (equal to sum of its proper divisors) excluding the number itself.
For example, 6 is a perfect number, since 1 + 2 + 3 = 6, where 1, 2, and 3 are all factors of 6. Similarly, 28 is a perfect number, since 1 + 2 + 4 + 7 + 14 = 28.
Examples:
CheckPerfect(6) ➞ true🏆 Leave your solutions in the comments. The solution will be posted below in a couple of hours 👇
CheckPerfect(28) ➞ true
CheckPerfect(496) ➞ true
CheckPerfect(12) ➞ false
CheckPerfect(97) ➞ false
#interview
📝 What are Property Accessors?
The get and set portions or blocks of a property are called accessors.
These are useful to restrict the accessibility of a property.
🔸 The set accessor specifies that we can assign a value to a private field in a property and without the set accessor property it is like a read-only field.
🔸 By the get accessor we can access the value of the private field. A Get accessor specifies that we can access the value of a field publicly.
#post
The get and set portions or blocks of a property are called accessors.
These are useful to restrict the accessibility of a property.
🔸 The set accessor specifies that we can assign a value to a private field in a property and without the set accessor property it is like a read-only field.
🔸 By the get accessor we can access the value of the private field. A Get accessor specifies that we can access the value of a field publicly.
#post
What will be the output of the following code snippet?
Anonymous Quiz
47%
40, 25000, 50000
6%
40, 25000, 25000
11%
40, 50000, 50000
36%
Error
📝 You have defined a destructor but it never executed. Why?
The runtime environment automatically invokes the destructor of a class to release the resources that are occupied by variables and methods of an object.
However, in C#, programmers cannot control the timing for invoking destructors, as Garbage Collector is only responsible for releasing the resources used by an object.
Garbage Collector automatically gets information about unreferenced objects from .NET's runtime environment and then invokes the
Although, it is not preferable to force Garbage Collector to perform garbage collection and retrieve all inaccessible memory (programmers can use the
Take notice that the destructor doesn't (and can't) have a public modifier in-front of it, it's sort of an hint that you can't explicitly call the destructor of an object.
#post
The runtime environment automatically invokes the destructor of a class to release the resources that are occupied by variables and methods of an object.
However, in C#, programmers cannot control the timing for invoking destructors, as Garbage Collector is only responsible for releasing the resources used by an object.
Garbage Collector automatically gets information about unreferenced objects from .NET's runtime environment and then invokes the
Finalize()
method.Although, it is not preferable to force Garbage Collector to perform garbage collection and retrieve all inaccessible memory (programmers can use the
Collect()
method of the Garbage Collector class to forcefully execute Garbage Collector).Take notice that the destructor doesn't (and can't) have a public modifier in-front of it, it's sort of an hint that you can't explicitly call the destructor of an object.
#post
#challenge
💻 Is the Input Factorial of an Integer? | #easy
Create a function that checks if a given integer is exactly the factorial of an integer or not. true if it is, false otherwise.
Examples:
#interview
💻 Is the Input Factorial of an Integer? | #easy
Create a function that checks if a given integer is exactly the factorial of an integer or not. true if it is, false otherwise.
Examples:
isFactorial(2) ➞ true🏆 Leave your solutions in the comments. The solution will be posted below in a couple of hours 👇
// 2 = 2 * 1 = 2!
isFactorial(27) ➞ false
isFactorial(24) ➞ true
// 24 = 4 * 3 * 2 * 1 = 4!
#interview
📝 What is the difference between a Struct and a Class in C#?
Class and struct both are the user defined data type but have some major difference:
Struct
🔸 The struct is value type in C# and it inherits from
🔸 Struct is usually used for smaller amounts of data.
🔸 Struct can't be inherited to other type.
🔸 A structure can't be abstract.
Class
🔸 The class is reference type in C# and it inherits from the
🔸 Classes are usually used for large amounts of data.
🔸 Classes can be inherited to other class.
🔸 A class can be abstract type.
🔸 We can create a default constructor.
#post
Class and struct both are the user defined data type but have some major difference:
Struct
🔸 The struct is value type in C# and it inherits from
System.Value
Type.🔸 Struct is usually used for smaller amounts of data.
🔸 Struct can't be inherited to other type.
🔸 A structure can't be abstract.
Class
🔸 The class is reference type in C# and it inherits from the
System.Object
Type.🔸 Classes are usually used for large amounts of data.
🔸 Classes can be inherited to other class.
🔸 A class can be abstract type.
🔸 We can create a default constructor.
#post
All of these interfaces inherit from
🔸 ICollection is the most basic of the interfaces you listed. It's an enumerable interface that supports a Count and that's about it.
🔸 IList is everything that ICollection is, but it also supports adding and removing items, retrieving items by index, etc. It's the most commonly-used interface for "lists of objects".
🔸 IQueryable is an enumerable interface that supports LINQ. You can always create an IQueryable from an IList and use LINQ to Objects, but you also find IQueryable used for deferred execution of SQL statements in LINQ to SQL and LINQ to Entities.
🔸 IDictionary is a different animal in the sense that it is a mapping of unique keys to values. It is also enumerable in that you can enumerate the key/value pairs, but otherwise it serves a different purpose than the others you listed.
#post
IEnumerable
. That interface basically lets you use the class in a foreach
statement (in C#).🔸 ICollection is the most basic of the interfaces you listed. It's an enumerable interface that supports a Count and that's about it.
🔸 IList is everything that ICollection is, but it also supports adding and removing items, retrieving items by index, etc. It's the most commonly-used interface for "lists of objects".
🔸 IQueryable is an enumerable interface that supports LINQ. You can always create an IQueryable from an IList and use LINQ to Objects, but you also find IQueryable used for deferred execution of SQL statements in LINQ to SQL and LINQ to Entities.
🔸 IDictionary is a different animal in the sense that it is a mapping of unique keys to values. It is also enumerable in that you can enumerate the key/value pairs, but otherwise it serves a different purpose than the others you listed.
#post
#challenge
💻 Largest Gap | #easy
Given an array of integers, return the largest gap between elements of the sorted version of that array.
Here's an illustrative example. Consider the array:
9, 4, 26, 26, 0, 0, 5, 20, 6, 25, 5
... which, after sorting, becomes the array:
0, 0, 4, 5, 5, 6, 9, 20, 25, 26, 26
... so that we now see that the largest gap in the array is the gap of 11 between 9 and 20.
Examples:
#interview
💻 Largest Gap | #easy
Given an array of integers, return the largest gap between elements of the sorted version of that array.
Here's an illustrative example. Consider the array:
9, 4, 26, 26, 0, 0, 5, 20, 6, 25, 5
... which, after sorting, becomes the array:
0, 0, 4, 5, 5, 6, 9, 20, 25, 26, 26
... so that we now see that the largest gap in the array is the gap of 11 between 9 and 20.
Examples:
LargestGap(new int[] { 9, 4, 26, 26, 0, 0, 5, 20, 6, 25, 5 }) ➞ 11🏆 Leave your solutions in the comments. The solution will be posted below in a couple of hours 👇
// After sorting get { 0, 0, 4, 5, 5, 6, 9, 20, 25, 26, 26 }
// Largest gap of 11 between 9 and 20
LargestGap(new int[] { 14, 13, 7, 1, 4, 12, 3, 7, 7, 12, 11, 5, 7 }) ➞ 4
// After sorting get { 1, 3, 4, 5, 7, 7, 7, 7, 11, 12, 12, 13, 14 }
// Largest gap of 4 between 7 and 11
#interview
✨ Here is a solution for the #challenge above
💬 At first blush, it seems tricky but, in my opinion, this solution is really graceful
💬 At first blush, it seems tricky but, in my opinion, this solution is really graceful
📝 What is an Abstract Class?
The
An Abstract class is a class which is denoted by
Abstract classes have the following features:
🔸 An abstract class cannot be instantiated.
🔸 An abstract class may contain abstract methods and accessors.
🔸 It is not possible to modify an abstract class with the sealed modifier because the two modifiers have opposite meanings. The
🔸 A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.
#post
The
abstract
modifier indicates that the thing being modified has a missing or incomplete implementation. The abstract modifier can be used with classes, methods, properties, indexers, and events.An Abstract class is a class which is denoted by
abstract
keyword and can be used only as a Base class.Abstract classes have the following features:
🔸 An abstract class cannot be instantiated.
🔸 An abstract class may contain abstract methods and accessors.
🔸 It is not possible to modify an abstract class with the sealed modifier because the two modifiers have opposite meanings. The
sealed
modifier prevents a class from being inherited and the abstract
modifier requires a class to be inherited.🔸 A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.
#post
What will be the output of the following code snippet?
Anonymous Quiz
4%
1 2 3 4 5
17%
11 12 13 14 15
78%
11 2 13 4 15
1%
1 3 5 7 9
📝 How to implement the Where method in C#?
The
The function returns an object that implements the
This is syntactic sugar introduced in C# 2.0. In earlier versions you had to create your own
#post
The
yield
keyword actually does quite a lot here. It creates a state machine "under the covers" that remembers where you were on each additional cycle of the function and picks up from there.The function returns an object that implements the
IEnumerable<T>
interface. If a calling function starts foreach
ing over this object, the function is called again until it "yields" result based on some predicate
.This is syntactic sugar introduced in C# 2.0. In earlier versions you had to create your own
IEnumerable
and IEnumerator
objects to do stuff like this.#post