עליך להתחבר בכדי להוריד קבצים מהאתר
על מנת להבטיח שמשאבינו לא ינוצלו לרעה.
Extension Method |
1 : static class ExtensionMethods
2 : {
3 : public static void WriteLine(this int num, ConsoleColor color)
4 : {
5 : ConsoleColor original = Console.ForegroundColor;
6 : Console.ForegroundColor = color;
7 : Console.WriteLine(num);
8 : Console.ForegroundColor = original;
9 : }
10 : }
11 : class Program
12 : {
13 : static void Main(string[] args)
14 : {
15 : int num = 12;
16 : num.WriteLine(ConsoleColor.Yellow);
17 : }
18 : }
1 : static class ExtensionMethods
2 : {
3 : public static bool IsNumeric(this string s)
4 : {
5 : double output;
6 : return double.TryParse(s, out output);
7 : }
8 : }
9 : class Program
10 : {
11 : static void Main(string[] args)
12 : {
13 : string str1 = "123.222";
14 : if (str1.IsNumeric())
15 : {
16 : Console.WriteLine("str1 is numeric");
17 : }
18 : }
19 : }
1 : class Person
2 : {
3 : public string FirstName { get; set; }
4 : public string LastName { get; set; }
5 : public byte Age { get; set; }
6 : public void Print()
7 : {
8 : Console.WriteLine(LastName + "," + FirstName + "," + Age);
9 : }
10 : public override string ToString()
11 : {
12 : return string.Format("Last name: {0}, First name: {1}, Age: {2}", LastName, FirstName, Age);
13 : }
14 : }
15 : static class ExtensionMethods
16 : {
17 : public static void PrintToFile(this object obj, string file_name)
18 : {
19 : System.IO.TextWriter tw = System.IO.File.AppendText(file_name);
20 : tw.WriteLine(obj.ToString());
21 : tw.Close();
22 : }
23 : }
24 : class Program
25 : {
26 : static void Main(string[] args)
27 : {
28 : string str = "Hello extension method!";
29 : str.PrintToFile("test.txt");
30 : int num = 1234;
31 : num.PrintToFile("test.txt");
32 : num = 5678;
33 : num.PrintToFile("test.txt");
34 : Person p = new Person() { FirstName = "Moshe", LastName = "Israeli", Age = 42 };
35 : p.PrintToFile("test.txt");
36 : DayOfWeek day = DayOfWeek.Monday;
37 : day.PrintToFile("test.txt");
38 : }
39 : }