Main Function
namespace ConsoleGenericsAssumption
{
class Program
{
static void Main(string[] args)
{
GenericTypeResolverTest v = new GenericTypeResolverTest("Hello Alam");
//v.Value = "Hello ALam";
v.ShowInConsole();
// -----same result, differnet implementation---
Console.WriteLine("-----same result, differnet implementation---");
string showString = v.ToString();
Console.WriteLine("{0}", showString);
Console.ReadLine();
// generic for string
string ValueString = "Hello Yaya";
ValueString.ShowInConsole();
// generic for int
int ValueInt = 12345;
ValueInt.ShowInConsole();
}
}
}
Another class
public class GenericTypeResolverTest
{
public string Value { get; set; }
public GenericTypeResolverTest() { }
public GenericTypeResolverTest(string value)
{
Value = value;
}
public override string ToString()
{
return Value.ToString();
}
}
public static class GenericTypeResolverMethodTest
{
public static void ShowInConsole(this T obj)
{
Console.WriteLine(obj.ToString());
Console.ReadLine();
}
}
imagefish 發表在 痞客邦 留言(0) 人氣()
static void Main(string[] args)
{
// [] is specific type and fixed length.
// It is a data structure.
String[] arr = new string[] {"Hi", "I", "am", "ALam"};
ShowMessage(arr);
// List is specific type and unbounded length.
// namespace is System.Collection.Generic
List list = new List();
list.Add("Hi");
list.Add("I");
list.Add("am");
list.Add("Alam");
ShowMessage(list);
// ArrayList is any type and undounded length.
// namespace is System.Collection
ArrayList arraylist = new ArrayList();
arraylist.Add("Hi");
arraylist.Add("I");
arraylist.Add("am");
arraylist.Add("Alam");
arraylist.Add(12345); // when we call the ShowMessage, it will be wrong.
ShowMessage(arraylist);
}
private static void ShowMessage(string[] temp)
{
foreach (string aaa in temp)
{
Console.WriteLine("{0}", aaa);
}
Console.ReadLine();
}
private static void ShowMessage(List temp)
{
foreach (string aaa in temp)
{
Console.WriteLine("{0}", aaa);
}
Console.ReadLine();
}
private static void ShowMessage(ArrayList temp)
{
foreach (string aaa in temp)
{
Console.WriteLine("{0}", aaa);
}
Console.ReadLine();
}
}
imagefish 發表在 痞客邦 留言(0) 人氣()
// to associates a stored procedure which user defined in the database.
// Must write it before func.
[global::System.Data.Linq.Mapping.FunctionAttribute(Name ="stored procedure name")]
// Undertake on a code, we make a func. let user to access the stored procedure.
// When SP exist input variables, you must use "Linq.Mapping.ParameterAttribute" to
// select the variable in DB and use "System.Nullable" to set
// what variable you wnat to assign in code.
public int storedProcedureForC#Class(
[global::System.Data.Linq.Mapping.ParameterAttribute(Name:"inputVarName",
DbType="inputVarType"] System.Nullable varInC#)
{
// Execute the stored procedure.
IExecuteResult objectForSP = this.ExecuteMethodCall(this, (
(MethodInfo)(MethodInfo.GetCurrentMethod())), varInC#);
return ((int)(result.ReturnValue));
}
imagefish 發表在 痞客邦 留言(0) 人氣()
So........We know "as" is a powerful method that perform certain types of conversions between compatible reference types. for example:
// ...... //
Array[] = dataTable;
// ....do something for dataTable.//
const string = "ALamTest";
Session[ALamTest] = dataTable;
//....do another something...//
Array[] getTable = Session[AlamTest] as Array[]
When we use Session Mechanism, "as" is a useful method let us to redesign the object which is taken out from the session.
imagefish 發表在 痞客邦 留言(0) 人氣()
This is a new Characteristic in C#3.0
namespace ConsoleUseExtensionMethod
{
public static class TestExtensionConsumer
{
public static void Main()
{
var s = "AlamInGamania";
// Extension Method
Console.WriteLine(s.WordCount());
Console.WriteLine("{0}{1}","效果同上",WordCount(s));
Console.ReadLine();
}
///summary
/// NonGenerics and must use static class
///summary
///<input> the input type is that want to extend </input>
///<return>the length of string</return>
public static int WordCount(this string v)
{
return v.Length; // Please "Note" Input parameter that The keyword "this".
}
}
}
If Type Built-in Functions has a same name with your func. name, C# 3.0 compiler will execute Built-in Function.
imagefish 發表在 痞客邦 留言(0) 人氣()