Virtual method can have its own implementation and abstract cannot.
Both methods can be overriden using the keywords override.
Virtual method can have its own implementation and abstract cannot.
Both methods can be overriden using the keywords override.
If you ever want to able to determine whether a value has been assigned. It is good to declare Nullable variable because it has hasvalue and value member to detect whether a value has been assigned. Value type such as int, long will give an error if you assign to Null but not nullable.
There are 2 ways to declare Nullable variable.
1. Nullable <bool> success=null;
2. bool? success=null;
if (success.hasvalue) Console.writeline(success.value)
Things you need to know about structure.
1. It is a value type and placed on a stack (class allocated on the manage heap).
structureA tempstruct=new structureA(1,2)
strucureA tempstruct2=tempstruct.
One of the example of structure is System.Drawing.Point
When to use structure instead of class.
1. Will not be changed after creation.
2. Has an instance size less than 16bytes.
3. Logically represents a single valuye.
4. Will not be cast to a reference type.
If the value of tempstruct change, it will not affect tempstruct2 because they are allocated/pointing at different piece of memory.
1. system.string is immutable. Any changes to the string causes runtime to create a new string and abandonĀ the old string. This causes unnecessay garbage collection. Use String concat, join, format and stringbuilder for better performance.
2. Creates Interface when you want multiple classes to behave similary and can be used interchangebly. Interface also known as contracts, define sets of members that all classes that implement the interface must provide. Visual studio 2005 has a refactor tools that extract interface from a custom class.
3. Partial class allow you to split a class definition across multiple source file. Partial class is new in .NET 2.0 and allow developer to split their tasks. A developer can work on the same class in different files.
4. Boxing assigns a value to reference type and unboxing assigns a reference type to value type.
Generic is new in .NET 2.0 and it allows you to define a type. Common uses are linked list, tree that doesn’t matter which type. Generic class reduced run-time errors and improved performance compare to Object class.
To create a Generic Type
Class <A,B>
{
public A a;
public B b;
public Gen(A m_a,B m_b)
{
a = m_a;
b = m_b;
}
}
To consume Generic type
Gen<string,string> genA=new Gen(“hello”,”friend”);
Limitation of using Generic class – you would limited to the capabilities of the base object class only.
To resolve this- use constraint “where”
class Gen<C>
where C:IComparable
then you can use icomparable members such as compareTo.
I wrote a sample delegates program with 3 buttons that explains it all.
Another good source of explaining delegates is in http://www.akadia.com/services/dotnet_delegates_and_events.html
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; //FooKean.wordpress.com namespace DelegateExample { public partial class Form1 : Form { public delegate void DelegateHandler(string tempstring); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } //Methods that has the same signature as delegatehandler public void DelegateMethod1(string tmpstring) { MessageBox.Show("This is delegate1. " + tmpstring); } public void DelegateMethod2(string tmpstring) { MessageBox.Show("This is delegate2. " + tmpstring); } private void btnDelegate_Click(object sender, EventArgs e) { //Delegate holds a reference to a method DelegateHandler dh = new DelegateHandler(DelegateMethod1); dh("method1"); } private void btnDelegate2_Click(object sender, EventArgs e) { DelegateHandler dh = new DelegateHandler(DelegateMethod2); dh("method2"); } private void btnErrHandler_Click(object sender, EventArgs e) { //demonstration of multi casting. //point to more than one function at a time. //this is the best part of delegates. DelegateHandler dh=null; dh += new DelegateHandler(DelegateMethod1); dh +=new DelegateHandler(DelegateMethod2); dh("method1"); dh("method2"); //the output will be 4 message boxes. //delegate 1 method 1 //delegate 2 method 2 //delegate 1 method 2 //delegate 2 method 2 //Delegate is useful when you want to log event to a file //and alert user at the same time. } } }
What are the common ways of storing application/user applications before Isolated Storage? .INI, registry, API and readable XML file. All of them have their shortcoming and I will not discuss here.
Isolated Storage is a special spot on the hard drive that only your app can find. I wrote a simple storage example program that illustrated this.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO.IsolatedStorage; using System.IO; //Implementing write and read to storage file. namespace IsolatedStorageExample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //Button create storage file private void btnCreate_Click(object sender, EventArgs e) { //Create an assembly/user-level store //each user has its own directory and each of them has its own assembly IsolatedStorageFile userStore = IsolatedStorageFile.GetUserStoreForAssembly(); //Create a file stream with a name userSettings.setting IsolatedStorageFileStream userStream = new IsolatedStorageFileStream("userSettings.setting", FileMode.Create, userStore); StreamWriter sw = new StreamWriter(userStream); sw.Write("This is a sample of storage."); sw.Close(); } //Read from storage file //Even when if I create the storage long ago, i can still read it as //long as it is there. private void btnRead_Click(object sender, EventArgs e) { IsolatedStorageFile userStore = IsolatedStorageFile.GetUserStoreForAssembly(); IsolatedStorageFileStream userStream = new IsolatedStorageFileStream("userSettings.setting", FileMode.Open, userStore); string[] files = userStore.GetFileNames("userSettings.setting"); if (files.Length != 0) foreach (String file in files) { StreamReader sr = new StreamReader(userStream); label1.Text = sr.ReadLine(); } } } }
When you want to add an item to Hashtable, there isn’t anyway you can access by the index. But this new fast dictionary enable you to access by index. Don’t be fool by the name “Ordered”, it is not sorted. It is almost the same with hashtable with 2 new methods – Insert(), RemoveAt()
private static void OrderedDictionaryExample() { OrderedDictionary gpa = new OrderedDictionary(); gpa.Add("80", "science"); gpa.Add("200", "advance science"); gpa.Add("90", "math"); gpa.Add("60", "nanoscience"); gpa.Add("100", "history"); gpa.Add("101", "music"); //insert by index gpa.Insert(0, "1000", "IQ test"); //Access by index for (int i = 0; i < gpa.Count; i++) { Console.WriteLine(gpa[i]); } //Access using dictionary entry foreach (DictionaryEntry de in gpa) { Console.WriteLine(de.Value); } Console.WriteLine("you got 101 score in " + gpa["music"]; //remove first item gpa.RemoveAt(0); }
The advantage is you don’t have to cast/convert it to string. It is a strictly string type only.
private static void StringDictionaryExample() { StringDictionary st = new StringDictionary(); st.Add("ipoh", "perak"); st.Add("negaragu", "negaragugu"); //Every books said it is case sensitive //but it tolower every string before //store it to hashtable. This is dumb! //st.Add("Negaragu","negaragugu"); //no conversion needed. string mycountry=st["negaragugu"]; } private static void StringCollectionExample() { StringCollection sc = new StringCollection(); sc.Add("Japan"); sc.Add("taiwan"); string country1 =sc[0]; }