Kamis, 20 Desember 2007

C# 3.0 Partial Method Ex


using System;


using System.Collections.Generic;


using System.Text;


namespace PartialMethod


{


class Program


{


public partial class MypartialClass


{


partial void MypartialMethod();


public void CallPartialMethod()


{


MypartialMethod();


}


}


public partial class MypartialClass


{


partial void MypartialMethod()


{


Console.WriteLine("Ronde kedua, baru di declare , belakangan");


}


}


static void Main(string[] args)


{


MypartialClass test = new MypartialClass();


test.CallPartialMethod();


Console.ReadLine();


}


}


}

Selasa, 18 Desember 2007

4 Ways declaring bool parameter , the last one is for c#3.0


this reference from msdn blog


using System;


using System.Collections.Generic;


using System.Text;


using System.IO;


namespace ConsoleApplication1


{


class Program


{


[Flags]


enum DisplayUserOptions


{


Email = 0x1,


PhoneNumber = 0x2,


Both = (Email | PhoneNumber),


}



static void Main(string[] args)


{


User user=new User();


TextWriter stream=null;


//cara 1


DisplayUser1(user, Console.Out, true /*displayEmail*/, false /*displayPhoneNumber*/);


//cara 2


DisplayUser2(user, stream, DisplayUserOptions.Email);


//cara 3


//More readable but much more verbose(panjang kata2nya)


DisplayUserOption options = new DisplayUserOption();


options.Email = true;


options.PhoneNumber = false;


DisplayUser3(user, stream,options);


//Cara 4 di C#3.0 bisa pake object initializers


DisplayUser3(user, Console.Out, new DisplayUserOption { Email = true, PhoneNumber = false });


}


static void DisplayUser1(User user, TextWriter stream, bool displayEmail, bool displayPhoneNumber)


{



}


static void DisplayUser2(User user, TextWriter stream, DisplayUserOptions options)


{ }


static void DisplayUser3(User user, TextWriter stream, DisplayUserOption options)


{ }


}


class User


{ }


struct DisplayUserOption


{


public bool Email;


public bool PhoneNumber;


}


}

Minggu, 09 Desember 2007

Decorative Pattern

The most Inner object , Pass the operation to outer object and so on.
This is a nice pattern approach.
here an example

using System;
namespace StarbuzzCoffee
{
class Program
{
static void Main(string[] args)
{
Beverage beverage = new Espresso();
beverage = new Mocca(beverage);
Console.WriteLine("{0} {1}", beverage.Getdescription(), beverage.Cost());

Beverage beverage2 = new Darkroast();
//the pattern is this is inheritance
//so the most inner object return the cost, the outer return + the inner
//and so on and so on till outside
beverage2 = new Mocca(beverage2);//see it change the base desc and
beverage2 = new Mocca(beverage2);//double mocha
Console.WriteLine(beverage2.description + " " + beverage2.Cost());
Console.ReadLine();

}
}


public abstract class Beverage
{
public string description="unknown beverage";
public virtual string Getdescription()
{
return description;
}
public abstract double Cost();
}

//public abstract class CondimentDecorator : Beverage
//{
// public abstract string Getdescription();
//}
public class Espresso : Beverage
{
public Espresso()
{
description = "Espresso";
}
public override double Cost()
{
return 1.99;
}
}
public class Darkroast : Beverage
{
public Darkroast()
{
description = "Darkroast";
}
public override double Cost()
{
return .22;
}
}
public class Houseblend : Beverage
{
public Houseblend()
{
description = "Houseblend";
}
public override double Cost()
{
return .89;
}
}
public class Mocca: Beverage
{
private Beverage beverage;
public Mocca(Beverage theBevarage)
{
beverage = theBevarage;

}
public override string Getdescription()
{
return beverage.Getdescription() + ",Mocha";
}

public override double Cost()
{
return .20 + beverage.Cost();
}
}
}

Kamis, 06 Desember 2007

Generic Constraint


using System;


using System.Windows.Forms;


namespace Generic101


{


public partial class Form1 : Form


{


public Form1()


{


InitializeComponent();


}


private void Form1_Load(object sender, EventArgs e)


{


Testclass testdoang = new Testclass();


MethodA(testdoang);


MethodB(testdoang);


TestClass<Chubby> damn = new TestClass<Chubby>();


}


public void MethodA<I>(I parameter1) where I : IComparable


{


parameter1.CompareTo(this);


}


public void MethodB(IComparable param1)


{


param1.CompareTo(this);


}


}


public class Testclass: IComparable


{


#region IComparable Members


public int CompareTo(object obj)


{


throw new Exception("The method or operation is not implemented.");


}


#endregion


}


public interface A


{


void method1();


void method2();


}


public interface B


{


void method1();


void method3();


void method4();


}


public class C


{


public void method1()


{


}


public void method3(){}


}


public class TestClass<T> where T:C,A,B,new()


{


public TestClass()


{


T aclass=new T();


aclass.method1();


aclass.method2();


aclass.method3();


aclass.method4();


}


}


public class Chubby: C,A,B


{


public Chubby()


{


}


public new void method1()


{}


public new void method3()


{ }


public void method2()


{


}


public void method4()


{


// throw new NotImplementedException();


}


}


}