C#学习笔记

参数:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 
 8 namespace HelloCSharp
 9 {
10      class Person
11     {
12         public string message(string msg,int x = 1)
13         {
14             return msg + x;
15         }
16         static void Main(string[] args)
17         {
18             
19             Person li = new Person();
20             //命名参数
21             Console.WriteLine(li.message(x:5,msg:"dd"));
22             //可选参数
23             Console.WriteLine(li.message("dd"));
24 
25             Console.ReadLine();
26         }
27     }
28 }
View Code

属性:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ParentClass
 8 {
 9     class Parent
10     {
11         private int  age;
12 
13         //属性是字段的自然扩展,本质上是定义修改字段的方法。
14         //只具有get访问器的属性称为只读属性,只具有set访问器的属性称为只写属性,两个都有的叫做读写属性。
15         //set访问器相当于具有单个名为value的参数和无返回类型的方法,get相当于无参数的有返回类型的方法。
16         public int Age
17         {
18             get
19             {
20                 return age;
21             }
22             set
23             {
24                // age = value;
25                 Console.WriteLine("您输入的值是: " + value);
26                 if (value > 0)
27                 {
28                     age = value;
29                 }
30                 else
31                 {
32                     Console.WriteLine("您输入的有误");
33                 }
34             }
35         }
36 
37         
38     }
39     class x
40     {
41         static void Main(string[] args)
42         {
43             Parent p = new Parent();
44             //Console.ReadLine();
45             p.Age = 12;
46             Console.WriteLine(p.Age);
47             p.Age = -1;
48             Console.WriteLine(p.Age);
49             Console.ReadLine();
50         }
51     }
52 }
View Code

索引:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ParentClass
 8 {
 9     class Parent
10     {
11         static void Main(string[] args)
12         {
13             SY sy = new SY();
14             sy[0] = 100;
15             sy[1] = 102;
16             Console.WriteLine(sy[1]);
17             Console.WriteLine(sy[100]);
18             Console.ReadLine();
19         }
20     }
21     class SY
22     {
23         private int[] array = new int[100];
24 
25         /*声明索引器  
26          *[修饰符] 数据类型 this[索引类型 index]
27          *{
28          *  get{}
29          *  set{}
30          * }
31           */
32         public int this[int index]
33         {
34             get
35             {
36                 if (index >= 0 && index < 100)
37                 {
38                     Console.WriteLine("OK");
39                     return array[index];
40                 }
41                 else
42                 {
43                     Console.WriteLine("Out of bound");
44                     return 0;
45                 }
46             }
47             set
48             {
49                 if (index >= 0 && index < 100)
50                 {
51                     array[index] = value;
52                 }
53             }
54         }
55     }
56 }
View Code

委托:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Delegate
 8 {
 9     public delegate void GreetingDelegate(String name);
10     class Program
11     {
12         private static void EnglishGreeting(String name)
13         {
14             Console.WriteLine("hello " + name);
15         }
16         private static void ChineseGreeting(String name)
17         {
18             Console.WriteLine("你好 " + name);
19         }
20         private static void GreetingPeople(String name, GreetingDelegate MakeGreeting)
21         {
22             MakeGreeting(name);
23         }
24         static void Main(string[] args)
25         {
26             GreetingPeople("liulanglang", ChineseGreeting);
27             Console.ReadKey();
28         }
29     }
30 }
View Code

FileCopy:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.IO;
 7 
 8 namespace Copy
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14 
15             string path_s = "source.txt";
16             string path_t = "target.txt";
17             try
18             {
19                 File.Delete(path_t);
20                 File.Copy(path_s, path_t);
21                 Console.ReadLine();
22 
23                 File.Copy(path_s, path_t); //不允许二次复制相同的文件,出错!
24             }
25             catch (Exception e)
26             {
27                 Console.WriteLine(e.ToString());
28                 Console.ReadLine();
29             }
30 
31         }
32     }
33 }
View Code

特性(Attributes):

公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型、字段、方法和属性等。
Attributes和Microsoft .NET Framework文件的元数据保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为。
特性可以放置在几乎所有的声明中。在 C# 中,特性的指定方法为:将括在方括号中的特性名置于其应用到的实体的声明上方。
自定义的Attribute:
[AttriubteUsage(参数设置)] public 自定义Attribute : Attribute { ... }

关键字default(T):

此关键字对于引用类型会返回空,对于数值类型会返回零。
对于结构,此关键字将返回初始化为零或空的每个结构成员,具体取决于这些结构是值类型还是引用类型。

在泛型中如果需要返回泛型类型的默认值则会用到这个关键字。
1. T是值类型而非结构的则default(T) 数值类型返回0,字符串返回空
2.T 是非引用类型是结构时候返回初始化为零或空的每个结构成员
3.引用类型返回NULL

其实就是为了返回默认值,比如int i =0;这样是可以的,但是int i=null是不可以的,但是泛型的时候不知道是值类型还是引用类型所以不知道如何赋默认值。
用这个关键字就解决了这个问题

原文地址:https://www.cnblogs.com/cjshuang/p/4984156.html