Csharp - o - logy

Sunday, 22 July 2012

I wonder Why ?

#1 We have "string" and "String" in C# when both does the same thing ?

Answer : Answer is fairly simple. string is an alias of String. Hows that ?. To achieve type safety, Microsoft has designed the Managed languages such that they can communicate with each other.

For the entire framework we have base types such as System.String and thus their code specific alias such as string in C# and String in VB. When we compile the program in C# it actually refers to System.String(Kinda how alias is suppose to work right)

#2 We use Stringbuilders over Strings ?

Answer: One word "MUTABLE". If we are writing a program and we have a lot of strings we want to manipulate we better go for Stringbuilders rather using the Strings.

Suppose we want to concatenate two strings.

string a = "Hello"; string b = " World"; string c = a + b;

What happens is "a" is created and "Hello" is stored in it. Then "b" is created and " World" is stored.Then "c" is created and a new instance of a is created and added with another new instance of b and and the concatenated value is stored in c. LOSS of good memory.

In case of StringBuilder we have .Append fucntion which when used, uses the same memory location to change the value. No new instance. Hence the word "Mutable"

#3 We use tilde operator ?

Answer: This little operator, tilde (~) operator is used as an Unary Operator otherwiser known as bitwise NOT operator.

What is that ? Look at this peice of code.

Here the output will always be equal as tilde operator performs the 2's Compliment on the number.

As per 2's compliment - ~ <number> = (- <number> ) - 1

#4 We cannot inherit Structures ? (Sorry I ran outta "Why" facts)

Answer: I searched this in the internet and found the answer. The answer is "You cannot inherit a sealed type". Why I am saying this ? Because, if you create a structure and try to inherit it in your class and build the application then you will see the error. "Man !! I should have first checked it in Visual Studio."

Monday, 16 July 2012

1. This is the process that runs when we run our application. This is a DAEMON Thread.
More : Garbage Collector is one of the two processes that runs by default when you run an applicaiton. Second is the Main program(Duh !!)

2. Garbage collector is only called when the application request memory and there is not enough memory.(Myth : It is always called.)

3. "Mark and Sweep" : This is a method comprises of the following.

GC assumes everything on the memory is a trash.

Prepares a graph of all the memory that is currently referenced by the application.

Compacts the heap by moving the memory in use to the start of the Heap.

The new heap is formed and the vacant is left and freed.

4. GC is only effective if performed on smaller memory objects. It will obviously have an adverse effect if the GC tries to move let say 30000 byte of memory.

5. To perform a programmer specific Garbage collection GC.Collect() is used. To use this we have to suppress the finalize method. Because if not suppresed, Garbage collector will work twice and performance will be surely adverse.

6. AAO TWIST KAREY : Dont think that GC.Collect() is called and it will destroy the object and will free up the memory. GC.Collect() has a proven high "Programmer Headache" rate. Please do not use it. Let .NET do all the garbage collection.

1. Interface
cannot inherit abstract class. In other words, An
interface can only inherit other interfaces.

2. Interface
can inherit multiple interfaces

3.
If you have two interfaces, the second one has inherited the first one,
if you inherit the second one to any class then all the methods from
the first interface will have to be declared too.

4. An interface member functions or variables cannot be declared with Access Modifiers.

5. If you have inherited an Interface, then you must declare all its method as Public when you are declaring.If declared as "Protected", "Private", "Internal" or "Protected Internal", you will see an error.