"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;
Showing posts with label C# Learning's. Show all posts
Showing posts with label C# Learning's. Show all posts

September 09, 2012

C# Fundamentals


This time is all about fundamentals of C# . Going beyond theoretical basics.

Tip #1 - Why Value Types are stored in Stack and not in Heap
This blog post (The Truth About Value Types) was the answer. It is a recommendation that CLR does on your behalf to store value types in stack. Short Lived storage (value types) live in stack. Long duration (reference types) live in heap

Tip #2 - Garbage Collection process, Phases Involved
Mark, Sweep and Compact. More details refer The Stack Is An Implementation Detail, Part Two

Tip #3 - Abstract Class VS Interfaces
  • Interface - Can define methods but not implement them
  • Abstarct Class - Can define/implement methods/members but cannot instantiate abstract classes
using System;
namespace examplecode
{
    abstract class AClass
    {
        int aMember;
        public void printVal()
        {
            Console.WriteLine("Example AbstractClass");
        }
    }
    interface iTest
    {
        void printValInterface();
    }
    class Program:AClass,iTest
    {
        public void printValInterface()
        {
            Console.WriteLine("Example printValInterface");
        }
        public static void Main()
        {
            Program RunProgram = new Program();
            RunProgram.printVal();
            RunProgram.printValInterface();
            Console.ReadLine();
        }
    }
}
 
Tip #4 - Why multiple inheritance is not supported in .NET
Answer from stackoverflow question (Why is Multiple Inheritance not allowed in Java or C#?) is realistic and impressive. The benefits are too less and adds a lot of complexity to support multiple inheritance.
 
Tip #5 - Disassembly Good Example
 
Couple of MSDN blog posts were very very impressive. A must read for every developer
 
Happy Learning!!!

May 13, 2011

C# Learnings Today

Revising C#. Yea I'm revising my C# basics. In this post we would discuss on
  •  Interfaces
  •  Generics
  •  LINQ
  • Threading in C# 
Value Type Vs Reference Type
  • Value Types - Stored in stack (int, float, double, decimal..)
  • Reference Type - Data Stored in Heap, Reference stored in Stack (Class, Interface, array, delegate)
Why Interfaces ?
  • Define methods but not implement them (Reference Type)
  • Cannot have Members 
  • Can Inherit Interfaces
Abstract Classes
  • Can contain regular, abstract and non abstract members
  • Cannot create instances of abstract classes
  • Abstract class can be derived from other abstract classes
When to use an Abstract Class and an Interface
  • Use an abstract class to provide default behavior
  • Abstract Class - creating a class library which will be widely distributed or reused
  • Use an interface to design a polymorphic hierarchy for value types  
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//Interface Example
interface IExample
{
    //Method Defined, Not Implemented
    int TestInterfaceMethod(int a, int b);
}

class MyTest : IExample
{
    public int TestInterfaceMethod(int a, int b)
    {
        return (a + b);
    }
}

abstract class abstractClassExample
{
    //Has both abstract and non abstract methods
    public void BaseMethod()
    {
           Console.WriteLine("I am Base Class");
    }
    abstract public void AbstractBaseMethod();
}
class ChildClass : abstractClassExample
{
    //Implement abstract method
    override public void AbstractBaseMethod()
    {
        Console.WriteLine("Child Class OverWritten");
    } 
}

class Program
{
    static void Main()
    {
        MyTest M = new MyTest();
        Console.WriteLine(M.TestInterfaceMethod(5, 10));
        ChildClass CC = new ChildClass();
        CC.BaseMethod();
        CC.AbstractBaseMethod();
        Console.ReadKey();
    }
}

Output

Why Generics ?
  • Allow you to execute code for multiple data types
System.Collections.Generic just like:
List<T>
Dictionary<K, V>
Queue<T>
Stack<T>
How do C# generics compare to C++ templates?

Why LINQ ?
  • Language Integrated Query
  • Query on data collection similar to sql query to databases
Why I should use Linq?
When should we use LINQ?
LINQ: Introducing The Skip Last Operators
SQL Bits Session - LINQ for DBAs and SQL Developers
LINQ Notes

Threading in C#
C# - Use of Multithread (when) - (Stack Overflow Question - Reposting it)
  • You can use threads when you need different execution paths
  • Allowing requests to be processed simultaneously
  • Making efficient use of an otherwise blocked CPU 
What is the difference between Deep Copy and Shallow Copy
  • Reposting it from msdn question
  • For instance, a Person object, containing references to an Address object, several Phone objects, etc.
  • If you make a shallow copy of that Person object you now have two Person objects, but they share the Address objects and Phone objects. In other words, you only make a new Person object, and then link it up to the existing Address and Phone objects.
  • A deep copy, on the other hand, will make new copies of everything, so that both Person objects now have their own set of Address and Phone objects. 
When is a shallow copy desirable (instead of a deep copy)? (Stack Overflow Question - Reposting it)
  • In C++, you might use deep copying to avoid difficult memory management issues
  • In Java/.NET, deep copying is not necessary for those reasons. Since the languages are both garbage collected
Static Methods Example
Still remember c basics. They still seem to hold true. Tested below code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static int i=0;
        static void TestMethod()
        {
            //only one copy exists throught the program
            //last modified value used in this case
            i++;
            Console.WriteLine("Value of it is " + i);
            Console.ReadKey();
        }
        void TestforValueMethod()
        {
            //Local variable hides global variable
            int i=0;
            Console.WriteLine("Value of i is " + i);
            i++;
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            //static method invoking without class creation
            TestMethod();
            TestMethod();
            TestMethod();
            Console.ReadKey();
            Program p = new Program();
            p.TestforValueMethod();
            TestMethod();
            Console.ReadKey();
           
        }
    }
}

Output is



More Reads
What is the difference between Arraylist.Copy() vs Arraylist.Clone()
ADO.NET: Building a Custom Data Provider for Use with the .NET Data Access Framework

Very Good Article Series for .NET Test Automation Series
I recommend Illustrated C# 2010 book. Awesome book.


Happy Reading!!