zoukankan      html  css  js  c++  java
  • C#中的各种关键字

    打算用这个多多的书写C#中的关键字的用处,希望大家也多多参与,我会尽量的总结道一起。

    1、关键字implicit、explicit 你懂的,用了他们你就可以也来个显示的转换(形如 int a=(int) 3.456)你写的类和其他数据了

    public sealed class Rational

    {

      publi Rational(Int32 num){}

      public Int32 ToInt32(){}

      //由一个Int32隐式构造并返回一个Rational

      public static implicit operator  Rational(Int32 num){return new Rational(num);}

      //由一个Rationa显示返回一个Int32

      public static explicit operator Int32(Rational r) { return r.ToInt32();}

    }

    2、关键字 this

      首先就是一个类中形如

    public class Bird

    {

      private name;

      public Bird(string strName){this.name=strName;}

    }

      然后就是扩展类中《CLR via C#》一书里面:

      public static class StringBuilderExtension

    {

       public static Int32 IndexOf(this StringBuilder sb,Char value)

    {

      for(Int32 index=0;index<sb.length;index++)

        if(sb[index]==value) return index;

      return -1;

    }

    }

      如此,当编译器可以看到 Int32 index=sb.IndexOf('X');  //VS会自动的将IndexOf注册到StringBuilder里面,以后StringBuilder对象智能感应就会出现IndexOf方法了。

    ※如果你觉得这篇文章不错,请点击推荐。如果你觉得我写的对你有用请关注我。
    作者:Max蚊子
    网站:feiger.cn         飞鸽博客,关注互联网、站长圈的程序员博客!
                 
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Nginx与Apache的对比
    gc buffer busy waits(ZT)
    Brocade SAN Switch Change Domain ID (ZT)
    Oracle异机恢复时报错ora19870 ora19507
    row cache lock (ZT)
    can a select block a truncate (ZT)
    NBU常用命令
    the RRD does not contain an RRA matching the chosen C
    Solaris10 x64安装64bit perl
    Solaris and Oracle 32bit Linking Error "fatal: symbol 'ntcontab'
  • 原文地址:https://www.cnblogs.com/kim01/p/2619120.html
Copyright © 2011-2022 走看看