码迷,mamicode.com
首页 > Windows程序 > 详细

C#学习笔记

时间:2015-09-06 12:56:28      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

1、C#程序结构

  C#是利用命名空间组织起来的,using将命名空间名所标识的命名空间内的类型成员导入当前编译单元中,从而可直接使用每个被导入的类型的标识符。

  Main方法是程序的入口点,C#中所有的Main方法都必须是静态的。

static void Main()
{
}

2、变量及类型

  装箱:将值类型转换为引用类型;拆箱:将引用类型转换为值类型。

  定义一个局部变量时,一定要对其初始化;尽可能少定义全局变量。

  布尔类型变量的值只能是true或false,不能与其他类型进行转换。

  Char在C#中表示一个Unicode字符。

  string是String的别名,表示Unicode字符的字符串;生成和构建一个长的字符串时,一定使用StringBuilder类型,而不用string类型。String对象是不可改变的,每次使用String类中的方法时都会在内存中创建一个新的字符串对象,这就需为该对象分配新的空间。若要修改字符串而不创建新的对象,则可使用StringBuilder类。

  集合类型:Array、ArrayList、Hashtable

3、属性和方法

  C#属性有两种:在公共语言运行库中定义的属性(特性)、自定义属性(get和set访问器),如下所示:

技术分享
namespace CSharpTest
{
    [DefaultPropertyAttribute("StationName")]
    class Station
    {
        private string _StationName;
        private double _Lon = 103;
        private double _Lat = 38;
        private Color _color;
        private string _file = string.Empty;
        private Font _font;

        [CategoryAttribute("常规"), DescriptionAttribute("文件名"), ReadOnlyAttribute(true)]
        public string FileName
        {
            get { return _file; }
            set { _file = value; }
        }
        [CategoryAttribute("显示"), DescriptionAttribute("颜色"), DisplayNameAttribute("颜色")]
        public Color Color
        {
            get { return _color; }
            set { _color = value; }
        }
        [CategoryAttribute("显示"), DescriptionAttribute("字体")]
        public Font Font
        {
            get { return _font; }
            set { _font = value; }
        }

        public string StationName
        {
            get { return _StationName; }
            set { _StationName = value; }
        }
        public double Lon
        {
            get { return _Lon; }
            set { _Lon = value; }
        }
        public double Lat
        {
            get { return _Lat; }
            set { _Lat = value; }
        }  
    }
}
View Code

  get访问器和方法体相似,须返回属性类型的值;而set访问器类似于返回类型为void的方法,它使用称为value的隐式参数。

  类的实例化对象不能调用静态方法,必须直接使用类名调用。

4、结构、类和接口

  结构的实例化可以不使用new运算符;结构可以声明构造函数,但它们须带参数;在结构声明中,除非字段被声明为const或static,否则无法初始化。

  在没有对类实例化前,无法用类名调用类中的方法或字段。

  常用类修饰符:new、private、protected、public、internal(只有其所在的类才能访问)、abstract(抽象类)、sealed(密封类,不允许被继承)。 

  C#类特性:封装、继承(只支持单继承)、多态。

  接口用interface声明,通过类继承来实现。通过接口可实现多重继承,一个类虽只能继承一个基类,但可继承任意接口。

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test1
{
    //声明三个接口
    interface IPeople
    {
        string Name
        {
            get;
            set;
        }
        string Sex
        {
            get;
            set;
        }
    }
    interface ITeacher : IPeople
    {
        void teach();
    }
    interface IStudent : IPeople
    {
        void study();
    }
    //类继承自多个接口,并实现接口
    class Program:IPeople,ITeacher,IStudent 
    {
        string name = "";
        string sex = "";
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public string Sex
        {
            get
            {
                return sex;
            }
            set
            {
                sex = value;
            }
        }
        public void teach()
        {
            Console.WriteLine(Name + " " + Sex +" 教师");
        }
        public void study()
        {        
            Console.WriteLine(Name + " " + Sex +" 学生");
        }
        static void Main(string[] args)
        {
            Program program = new Program();//实例化对象
            ITeacher iteacher = program;
            iteacher.Name = "Tea";
            iteacher.Sex = "";
            iteacher.teach();
            IStudent istudent = program;
            istudent.Name = "Stu";
            istudent.Sex = "";
            istudent.study();
            Console.ReadKey();
        }
    }
}
View Code

  抽象类与接口的:

  • 抽象类与接口都不能直接实例化,但可声明它们的变量;
  • 抽象类中可定义成员的实现,但接口不可以;
  • 抽象类可包含字段、构造和析构函数、静态成员或常量等,但接口中不可以;接口可包含事件、索引器、方法和属性;
  • 接口中的成员必须是公共的;

  声明密封方法时,sealed修饰符总是和override修饰符同时使用。

  使用partial定义分部类,分部类的声明须与其他部分位于同一命名空间。

5、异常处理

  try...catch...finally语句

6、迭代器与泛型

  迭代器(iterator)有时又称游标(cursor),是可返回相同类型的值的有序序列的一段代码,是程序设计的软件设计模式(Design Pattern),可在容器上遍历接口,设计人员无需关心容器的内容。

  C#迭代器代码使用yield return语句依次返回每个元素,yield break语句终止迭代;其返回类型须为IEnumerable或IEnumerator中的任意一种。创建迭代器最常用的方法是对IEnumerator接口实现GetEnumerator方法。

  泛型是用于处理算法、数据结构的一种编程方法,主要是为了提高代码的重用性。泛型的使用如下所示:

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Finder
{
    public class Finder
    {
        public static int Find<T>(T[] items, T item)
        {
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i].Equals(item))
                {
                    return i;
                }
            }
            return -1;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int i = Finder.Find<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 6);
            Console.WriteLine("6 在数组中的位置:" + i.ToString());
            Console.ReadKey();
        }
    }
}
View Code

7、IO操作

  文件、文件夹、XML、ini配置文件、注册表、数据库

8、网路编程

  Sockets、Mail、Web

9、线程

10、窗体

  Form窗体、MDI窗体、各种控件(ListBox、TreeView、DockPanel、水晶报表、打印)、GDI+图形图像技术

C#学习笔记

标签:

原文地址:http://www.cnblogs.com/gaohongchen01/p/4784814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!