[DotNet].NET and C# fundamentals

1. Why and when to use Inheritance.

    a. As a class user, we can create a new class inherited from parent's class. When we use an IDE such as Visual Studio, IDE will auto complete some code for us. - Relationship
    b. As a class user, we can reuse other's already defined classes, so that save man power to recreate wheal.  - Reuse.
    c. Parent class can handle child instance, but child class cannot handle parent instance. - Polymorphism
    d. Sometimes we want to change a global class to change all children classes. - Global control

2. What is overriding and overloading.

Overriding: Child class has the same method name as in parent class, but child class uses its own method implementation.
Overloading: in one class, we can define methods with the same name but with different parameters numbers.

3. Abstract class vs. interface.

Once we ship our interface to customers, we won't have a chance to change it anymore. If we change our interfaces in the later versions, customers' code implement the old interface will no longer works under the new version framework. However, if we ship our code as classes, when there is a new version code to ship. We can add our new methods and properties to old classes and keep old methods unchanged, so that even customers' old code will still works with our new version framework.

So, Prefer to using classes instead of interface, unless you need multi-inheritance. By defining classes, we are able to add new members to old classes without breaking the existing code.

Define interface for some ability, these interfaces would end up with "-able". For example, Value types cannot inherit from other types, but can implement interfaces; we can define IComparable, IFormattable for these value types.

When we define Abstract classes, we say the inherited class "IS" something, when we define interfaces, we say the implemented class "Can DO" something.

4. Why do we need Abstract class?

Before learning why, we need to understand what abstract class is. An abstract class is a class but:

a. cannot be instantiated;
b. may contain abstract methods; ("may" means abstract class can also include real methods).
c. a normal class derived from an abstract must include all actual implementation.
d. cannot be sealed(an abstract with a sealed keywords is useless)

We use abstract class to define class contract. With or without real implementation.

5. Abstract class design rule.

Constructors should be protected, so that only its sub class can call it. It is a bad design if we define public constructors. Since abstract class cannot be instantiated, it will be a mislead to users to define a public constructors.

6. C# access modifiers

public – all
internal – limited to assembly
protected – itself or its subclasses.
protected internal – (protected or internal)
private – limited to itself.

7. What is the difference between Dispose and Finalize

Dispose is a method defined in IDisposable interface.
Dispose is called by developers, Finalize is called by GC.

Both Dispose and Finalize methods are used to free unmanaged resource.

We cannot rely on Finalize to clean up unmanaged resources. Because some objects would be promote to higher generation and won't be GCed frequently. That is why sometimes we need implement Dispose method. And call this method manually.

Finalize sample:

    class Test {
        //code
        ~Test() {
            //release unmanaged resource.
        }
    }

Dispose sample:

    class Test : IDisposable {
        private bool isDisposed = false;
        ~Test() {
            Dispose(false);
        }

// this method cannot be called publicly
        protected void Dispose(bool disposing) {
            if (disposing) {
                //free managed resource
            }
            //free unmanaged resource
            isDisposed = true;
        }
        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }



When we call Dispose manually, there is no need for GC to clean up the same object. That is why we call GC.SuppressFinalize(this) . A performance consideration.

8. How GC works

GC searches managed objects that are referenced, then GC attempts to finalize objects that are not referenced. Lastly, GC frees unreferenced objects and reclaims memory.

GC managed objects in "generations", new created objects is in lower generation. GC runs much more frequently to clean up lower generation objects.

9. What is Boxing and Unboxing?

Boxing : value type -> reference type
Unboxing: reference type-> value type

10. Heap and Stack

a. Reference types always allocated in heap.
b. Value type allocated to where it is declared, sometimes stack, sometimes heap
c. Pointer(reference type) can be allocated where it is declared.
d. Instructions tell cpu how to use these data in stack or heap.
For more, check this link:
http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx?ArticleID=9adb0e3c-b3f6-40b5-98b5-413b6d348b91

11. Why String in .NET is designed to be immutable

For example, when we create a string "good night". in c#, strings are Unicode compiled. so Windows will allocate 10*2=20 bytes for you. now, there is a continuous memory array where stored the string. please also note that string is actually a char array in the memmory. then, you want to add some additional characters to the string. however, the memmory space after the string is already allocated by Windows. What you going to do next.
      1. add the additional characters to some other place.
      2. cancel all and relocate a larger space for the new string.
If we choose option 1. the string's continous will be demaged, and it will not easy to find the new added characters. so, obviously, option 2 is the only choice. that is why string in C# and Java is immutable.

12. What is the difference between Process and Thread

Long long time ago, there are no Process, and Thread either. Programmers punched a card and input to a computer. cpu read data and operations from the card and execute it. ,while the cpu calculation frequence kept increase,  people see that it is really a waste for a cpu to do one thing at a time. why not let cpu handle several different task parallelly. so, there came out the Process. Every Process has its own virtual memory space. every running process ocuppy a very little timespan of cpu.different processes shift so quick that it seems they are running at the same time. a demon process will monitor other processes and manage these processes. the demon process is the most early operation system(OS).the first demon program is hard to create and very expensive.

Things runs well in the DOS time. However, world changed when a Visual OS come out. people see that it was really difficult to let a process to handle all the graphic drawing and program logic. there must be some thing that can make several code segments run at the same time even in one Process. there came out Thread. a thread is a small process inside a Process.

Since then, every process contains at least one primary thread and programmers can create other threads to handle program logic. however, Every process need to manage these threads, and creating a thread need time and memory space. threads don't have its own virtual memory space. threads in one process share one virtual memory space defined by the process.

In the hard core, it is not the process execute threads, cpu does the job.  All Process and Thread are not actually any real thing. they are abstract concepts. A process in Windows may differ from one in Mac.

13. What is the maximum memory a Windows Process can address?

A normal 32 bit process can address at most 4GB memory. the upper 2GB address is reserved for Windows. the left 2GB address can be used by the process itself.
What I am talking now is the virtual memory address. not the real data in memory. real data could be stored in RAM or hard disc or on both of them. Windows will map the virtual memory address to the physical memory address.  no application access to the real physical memory directly.

blog comments powered by Disqus