• C#小知识点


    1、显示|隐示转换:

     public static explicit operator ImplicitClass(ExplicitClass explicitClass)   //implicit  
            {
                ImplicitClass returnClass = new ImplicitClass();
                returnClass.iVal = (int)explicitClass.dVal;
                return returnClass;
            }
    

    2、默认的构造函数在非默认构造函数之前调用:this()

      public Class(Type val)
                : this()
            {...}
    

     3、列举类内的字段、方法等信息

    Type t = typeof(ExampleClass); 
          // Alternatively, you could use 
          // ExampleClass obj = new ExampleClass(); 
          // Type t = obj.GetType(); 
     
          Console.WriteLine("Methods:"); 
          System.Reflection.MethodInfo[] methodInfo = t.GetMethods(); 
     
          foreach (System.Reflection.MethodInfo mInfo in methodInfo) 
             Console.WriteLine(mInfo.ToString()); 
     
          Console.WriteLine("Members:"); 
          System.Reflection.MemberInfo[] memberInfo = t.GetMembers(); 
     
          foreach (System.Reflection.MemberInfo mInfo in memberInfo) 
             Console.WriteLine(mInfo.ToString()); 

     3、关于枚举类型

      1) 所有枚举类型(enum type)都是值类型。
      2) System.Enum和System.ValueType本身是引用类型。
      3) 枚举类型(enum type)都是隐式的直接继承自System.Enum,并且这种继承关系只能由编译器自动展开。但System.Enum本身不是枚举类型(enum type)。
      4)System.Enum是一个特例,它直接继承自System.ValueType但本身却是一个引用类型。

     4、判断窗体包含的控件类型:

     foreach(Control cont in from.Controls)
       {
        switch(cont .GetType().ToString())
        {
         case "System.Windows.Forms.Label":
                break;
         case "System.Windows.Forms.Panel":
          break;
         case "System.Windows.Forms.GroupBox":
          break;
        }
         
       }
    View Code

     5、依据方法名调用方法:

                System.Reflection.MethodInfo method = this.GetType().GetMethod("GetCard");
                if (null != method)
                {
                    method.Invoke(this, new object { sender,EventArgs.Empty});
                }
    View Code

     6、打开文件夹、具体文件、应用程序等

    System.Diagnostics.Process.Start(@path);  

     7、自己绘制面板,必须把面板的Style属性设置为0wnerDraw,并处则StatusBar的DrawItem事件。

     8、richtextbox控件使用LoadFile(“../test.rtf”)加载,使用SaveFile(“../test.rtf”)保存文件;

      打开richtextbox内容中的链接文本:

    private void richTextBoxText_LinkClicked(object sender, LinkClickedEventArgs e)
        {
          System.Diagnostics.Process.Start(e.LinkText);
        }
    View Code

     9、激活ListView内项的事件是ItemActivate

    10、Textbox输入数字或退格,其他字符无效(Enter键码:13)

    private void textBoxAge_KeyPress(object sender, KeyPressEventArgs e)
        {
          if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            e.Handled = true; // Remove the character
        }
    View Code

     11、新建字体(改变字体大小)

    FontFamily currentFontFamily = this.richTextBoxText.SelectionFont.FontFamily;
    Font  newFont = new Font(currentFontFamily, newSize);

     12、窗体颜色(默认):System.Drawing.SystemColors.Window

     13、若try...catch...finally中的catch块抛出异常(throw),则该异常不会由当前的try...catch...finally块处理,而是由一级的代码处理(但try...catch...finally中的finally块仍会执行)

    14、using 代码块中使用变量<VariableName>(初始化资源对象),会在这个代码块的末尾自动调用Dispose()方法。

    15、类适用与Foreach语句,则须实现GetEnumerator()函数

    //求两数之间的素数
    
          public IEnumerator GetEnumerator()
          {
             for (long possiblePrime = min; possiblePrime <= max; possiblePrime++)
             {
                bool isPrime = true;
                for (long possibleFactor = 2; possibleFactor <=
                   (long)Math.Floor(Math.Sqrt(possiblePrime)); possibleFactor++)
                {
                   long remainderAfterDivision = possiblePrime % possibleFactor;
                   if (remainderAfterDivision == 0)
                   {
                      isPrime = false;
                      break;
                   }
                }
    
                if (isPrime)
                {
                   yield return possiblePrime;
                }
             }
          }
    
    //foreach (long i in primesFrom2To1000)
    View Code

     16、类适用于List.sort(),则须继承ICompare接口,并实现Compare函数。

    17、类集合:1)继承CollectionBase :List ;2)继承DictionaryBase:Dictionary 3)继承: List<Class>

    18、yield return:

    static IEnumerable SimpleList()
          {
             yield return "string 1";
             yield return "string 2";
             yield return "string 3";
          }
    
     foreach (string item in SimpleList())
                Console.WriteLine(item);
    View Code

     19、类型比较:obj.GetType()==typeof(Type)

    20、表示比较同一类型的两个对象的方法:public delegate int Comparison<in T>(T x, T y);

    Comparison<Type> sorter = new Comparison<Type>(Func_Compare);
                m_List.Sort(sorter);
    View Code

    21、表示定义一组条件并确定指定对象是否符合这些条件的方法:public delegate bool Predicate<in T>(T obj);

    Predicate<Type> searcher =new Predicate<Type>(bool_Func);
                Types ts= new Types(m_List.FindAll(searcher));
    View Code

     22、泛型 :

    using System.Collections;
    using System.Collections.Generic;
    
     public class Farm<T> : IEnumerable<T>
              where T : Animal
        {
            private List<T> animals = new List<T>();
    
            public IEnumerator<T> GetEnumerator()
            {
                return animals.GetEnumerator();
            }
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return animals.GetEnumerator();
            }
    }
    View Code

     23、初始化器

    public class Farm<T> : IEnumerable<T>    //公开枚举器
             where T : Animal
       {
          public void Add(T animal)    //辅助类初始化器
          {
             animals.Add(animal);
          }
    
          private List<T> animals = new List<T>();
    }
    
    //类初始化器
    Farm<Animal> farm = new Farm<Animal>
        {
            new Cow { Name="Norris" }
        };
    View Code

    24、类型推理:var <varName> = <value>; var并不是一种类型,因此并未违背C#的强类型化。

      1)必须初始化变量。

      2)相同的类型;相同的引用类型或空;所有元素的类型都可以隐式地转换为一个类型。

      3)var标识符并非不能用于类名。如果代码在其作用域中有一个Var类,就不能使用var关键字的隐式类型转化功能。

    25、匿名类型:使用C#编译器根据要存储的数据自动创建类型,而不是定义简单的数据存储类型。

      1)匿名实例的属性是只读的。

      2)==比较引用,而equals比较值

    var curries = new[]
             {
                new
                {
                   MainIngredient = "Lamb"
                },
                new
                {
                   MainIngredient = "Chicken"
                }
             };
    View Code

     26、动态查找:

      1)动态变量是类型不固定的变量。

      2)动态查找功能由Dynamic Language Runtime(动态语言运行库)支持。.Net4的一部分。

      3)dynamic类型仅在编译期间存在,在运行期间会被System.Object类型替代。

    using Microsoft.CSharp.RuntimeBinder;
    static dynamic GetValue()
          {}

     27、delegate :具有双重含义,匿名方法和定义委托类型都使用它。

    28、默认参数:给指定的参数赋值varName=value,参数顺序改变时varName:value;

    29、扩展方法:可以扩展类型的功能,但无需修改类型本身。甚至可以使用扩展方法扩展不能修改的类型,包扩.NetFramework中定义的类型。

      扩展方法的语法要求

      1)方法必须是静态的。

      2)方法必须包含一个参数,表示调用扩展方法的类型实例。(实例参数)

      3)实例参数必须是为方法定义的第一个参数。

      4)实例参数仅有this修饰符。

    //定义
    public static List<string> GetWords(
             this string sentence
             )
          {
            
          }
    
       
          public static string ToStringReversed(this object inputObject)
          {
           
          }
    
          public static string AsSentence(this List<string> words)
          {
             
          }
    //调用
    sourceString.GetWords().AsSentence();
    sourceString.Length.ToStringReversed();
    View Code

     30、菜单项点击时显示是否选择状态(对勾),必须先将其属性中的CheckedOnClick属性设置为True.

    31、子窗口的布局:在窗体属性中将IsMdiContainer设置为true.

      1)Forms命名空间下,自定义了枚举MdiLayout:指定 MDI 父窗口中多文档界面 (MDI) 子窗口的布局。

      2)Form类自定义了函数:LayoutMdi():在 MDI 父窗体内排列多文档界面 (MDI) 子窗体。

      3)子窗体属性MdiParent:获取或设置此窗体的当前多文档界面 (MDI) 父窗体。

    32、多窗口时菜单设置:

      1)菜单属性AllowMerge:指示能否将父菜单与子菜单的菜单栏及菜单项进行合并,默认为True。

      2)菜单属性MdiWindowListItem:指示显示父窗体中打开子窗体列表的顶级菜单项。

      3)菜单项属性MergeAction:获取或设置如何将子菜单与父菜单合并。

      4)菜单项属性MergeIndex:获取或设置合并的项在当前ToolStrip内的位置。

    33、部署Windows应用程序

      1)在ClickOnce部署中,可以通过单击某个网站的链接来安装应用程序。在客户启动或应用程序时,更新过程自动进行。如果需要在全局程序集缓存上安装共享组件,用户选择在其中安装应用程序的目录,或如果需要一些注册表项,就应使用Windows安装程序部署选项。

      2)变写SilverLight应用程序是为多客户应用程序提供基于Web的部署的一个选项。。。

    34、安装应用程序的用户可以使用证书来辨识安装软件包的创建者。阅读证书的内容,可以确定是否能信任此安装软件包。

  • 相关阅读:
    Codeforces714C【映射】
    Codeforces712C【贪心】
    Codeforces712B【= =】
    lightoj1259 【素数预处理】
    Codeforces482B【线段树构造】
    51nod 1348【next_permutation】
    hdoj5289【RMQ+二分】【未完待续】
    hdoj5875【二分+RMQ】
    RMQ算法
    AtCoder Regular Contest 061 DSnuke's Coloring
  • 原文地址:https://www.cnblogs.com/shenchao/p/4421688.html
Copyright © 2020-2023  润新知