zoukankan      html  css  js  c++  java
  • C# 编程数据结构学习笔记 2

    1《委托》

      委托就是将方法作为参数一样的去使用

      类的使用就是先声明然后再去使用。

      所以我们在使用委托的时候就是先去声明委托定义委托告诉编译器我们使用的委托的类型是什么类型的方法,然后创建委托的实例。

    代码如下:声明 - 创建实例-传入x方法-使用委托实例调用方法

    class Program
    {
    private delegate string GetAString(); //定义了一个委托的类型
    static void Main(string[] args)
    {
    int x = 40;
    GetAString a = new GetAString(x.ToString);     //a指向了ToString方法  GetAString a = x.ToString();
    string s = a(); //通过委托实例来调用方法  //String s = a.Invoke();
    Console.WriteLine(s);
    Console.ReadKey();
    }
    }

    2:<将委托类型当做一个参数来使用>:

    static void Main(string[] args)
    {

    PrintString method = Method1;
    PrintStr(method);
    Console.ReadKey();
    }
    private delegate void PrintString();

    static void PrintStr(PrintString print)
    {
    print();
    }
    static void Method1()
    {
    Console.WriteLine("Method1");
    }

    3:<action>委托

     action委托是一种我们称为无返回值类型的委托void 但是可以为其加入泛型来增加别的类型

    4:《func》委托

     必须带返回值类型的委托

    static int Test1()
    {
    return 1;
    }

    static void Main(string[] args)
    {
    Func<int> a = Test1;
    Console.WriteLine(a());
    Console.ReadKey();
    }
    }

      4.《对int类型的进行排序(冒泡排序)》

    namespace 冒泡排序
    {
    class MainClass
    {
    static void Sort(int[] sortArray)
    {
    bool swapped = true;
    do {
    swapped = false;

    for (int i = 0; i < sortArray.Length-1; i++) {

    if (sortArray[i]>sortArray[i+1]) {
    int temp = sortArray[i];
    sortArray[i]=sortArray[i+1];
    sortArray[i+1]=temp;
    swapped = true;
    }
    }

    } while (swapped);
    }

    public static void Main (string[] args)
    {
    int[] a = new int[]{ 1, 23, 4, 8, 67, 3, 56 };
    Sort (a);
    foreach (var item in a) {
    Console.WriteLine (item);
    }
    Console.ReadKey ();
    }
    }
    }

  • 相关阅读:
    MySQL数据库的常用命令
    函数返回 局部变量问题
    几种网络I/O模型
    socket的select模型【摘】
    Linux Bash Shell入门教程【转载】
    Shell学习【转】
    unicode 2 utf8 [转]
    linux下多线程的创建与等待详解 【转载】
    运算(93&-8)的结果
    一道腾讯的面试题,关于a和&a
  • 原文地址:https://www.cnblogs.com/ylllove/p/6725050.html
Copyright © 2011-2022 走看看