c#

Posted on 2019-01-29 19:03  金色的省略号  阅读(238)  评论(0编辑  收藏  举报

.NET Framework组织结构

主要包括三部分:

1、语言 公共语言规范  2、.NET Framework类库  3、公共语言运行库CLR(托管和执行代码)

 

公共语言运行时

CLR    Common Language Runtime    相当于java中的虚拟机

 

 

.NET开发工具

.NET Framework SDK

.NET Framework SDK 包括了微软提供的.NET 语言编译器(如C#编译器CSC.exe和VB.NET编译器VBC.exe),完整的.NET Framework文档,以及各种工具(如AL.exe和SN.exe等)

The Windows SDK provides tools, compilers, headers, libraries, code samples, and a new help system that developers can use to create applications that run on Microsoft Windows. You can use the Windows SDK to write applications using the native (Win32/COM) or managed (.NET Framework) programming model. 

 

.NET Framework与.NET Framework SDK的区别

只有一句话,如果只安装了.NET Framework,只能运行.NET应用程序,而不能开发.NET应用程序。

 .NET Framework包含了CLR(Common Language Runtiem,公共语言运行时)、BCL(Base Class Library,基类库)以及相关的语言编辑器等工具。

 

.NET Framework类库

 

面向对象

类  字段 方法 属性 索引 委托 事件

属性是对字段、方法的扩展

button1.Text = "Hello";  相当于button1.SetText("Hello");
string s = button1.Text;  相当于string s = button1.GetText();

 1 using System;
 2 class Test
 3 {
 4     private string _name = "test";
 5     public string Name//设置属性Name
 6     {
 7         get{
 8             return _name;
 9         }
10         set{
11             _name = value;//关键字value代表传进去的值
12         }
13     }
14 
15     /*    设置属性的代码,编译器实际上是编译成两个方法 
16         string get_Name();
17         void set_Name(string value);  */
18     
19     static void Main( string [] args )
20     {
21         Test t = new Test();
22         Console.Write(t.Name);//属性值
23         Console.ReadLine();
24     }
25 }
类的属性1
 1 using System;
 2 class Test
 3 {
 4     private string _name = "test";
 5     private int _age = 20;
 6     public string Info//设置属性Info
 7     {
 8         get{
 9             return "Name:" + _name + ",Age:" + _age;
10         }
11     }
12 
13     static void Main( string [] args )
14     {
15         Test t = new Test();
16         Console.Write(t.Info);//属性值
17         Console.ReadLine();
18     }
19 }
类的属性2
 1 using System;
 2 class Test
 3 {
 4     private string _name = "No";
 5     /*使用属性简写,会生成一个新的字段,
 6     不会读取、改写_name*/
 7     public string Name
 8     {
 9         get;
10         set;
11     }
12 
13     public string getName()
14     {
15         return _name;
16     }
17 
18     static void Main( string [] args )
19     {
20         Test t = new Test();
21         Console.WriteLine(t.Name);//属性值  空
22         t.Name = "Yes";
23         Console.WriteLine(t.getName());//字段值 "No" 
24         Console.WriteLine(t.Name);//属性值 "Yes"   
25         Console.ReadLine();
26     }
27 }
类的属性3

 索引(indexer)是使得对象可以像数组一样被索引的成员。

 索引使得类似数组的访问变得可能。

 1 using System;
 2 class IndexerRecord
 3 {
 4     private string [] data = new string [6];
 5     private string [] keys = {
 6         "Author", "Publisher", "Title",
 7         "Subject", "ISBN", "Comments"
 8     };
 9     public string this[ int idx ] //索引器
10     {
11         set
12         {
13             if( idx >= 0 && idx < data.Length )
14                 data[ idx ] = value;
15         }
16         get
17         {
18             if( idx >= 0 && idx < data.Length )
19                 return data[ idx ];
20             return null;
21         }
22     }
23     public string this[ string key ] //索引器重载
24     {
25         set
26         {
27             int idx = FindKey( key );
28             this[ idx ] = value;
29         }
30         get
31         {
32             return this[ FindKey(key) ];
33         }
34     }
35     private int FindKey( string key ) //查找key函数 
36     {
37         for( int i=0; i<keys.Length; i++)
38             if( keys[i] == key ) return i;
39         return -1;
40     }
41     static void Main()
42     {
43         IndexerRecord record = new IndexerRecord();
44         // record[ 0 ] = "马克-吐温";
45         // record[ 1 ] = "Crox出版公司";
46         // record[ 2 ] = "汤姆-索亚历险记";
47         record[ "Title" ] = "马克-吐温";
48         record[ "Author" ] = "Crox出版公司";
49         record[ "Publisher" ] = "汤姆-索亚历险记";
50         Console.WriteLine( record[ "Title" ] );
51         Console.WriteLine( record[ "Author" ] );
52         Console.WriteLine( record[ "Publisher" ] );
53         Console.ReadLine();
54     }
55 }
索引代码