zoukankan      html  css  js  c++  java
  • C#小知识

    1、<< 、<<= 、>> 、>>的用法以及区别 

    <<(左位移):

    例如   int a = 2 ;    b = a << n ;  如果 n=3,那么 b=16

    左移n位实际上相当于乘以2的n次方:

    for(int i =1;i<=n;i++)

      a = a * 2 ;

    >>(左位移):

    右移n位实际上相当于被2的n次方整除:

    for(int i =1;i<=n;i++)

      a = a / 2 ;

    <<=:

    a<<=1等价于a=a<<1,就是将a按位左移后将值赋值给a

    a<<1只是将a按位左移,并不改变a的值

    2、Linq表达式

                var b = array.Where(i => i > 0).OrderBy(i => i).Select(i => "[" + i + "]");
                foreach (string i in b)
                {
                    Console.WriteLine(i + "   ");
                }
    
                var shen = from i in array
                           where i > 0
                           orderby i
                           select "[" + i + "]";
    
                int[] values = { 1, 2, 5, 2, 3, 5, 5, 3, 4, 3, 3 };
                var all = from i in values
                          group i by i into g
                          orderby g.Count() descending
                          select new { 数字 = g.Key, 数量 = g.Count() };
                foreach (var q in all.Take(3))
                {
                    Console.WriteLine(q.数字 + "==" + q.数量);
                }

     3、使用csc.exe编译器

    csc.exe编译器一般目录在c:windowsMicrosoft.NETFrameworkv3.5下,但是一般在环境变量下设置好了,然后就可以在dos下编译你的文件了!

    csc d: est.cs 即可!就会生成对应的exe文件了,一般是控制台应用程序!

    4、 C#中的#if, #elif, #else和#endif预处理指令

    #if, #elif, #else, #endif

    #if  condition1
          code1
    #elif condition2
          code2
    ...
    #elif conditionn
          coden
    #else
          code(n+1)
    #endif

    当编译器遇到#if语句后,将先检查相关的符号是否存在,如果符号存在,就只编译#if块中的代码。否则,编译器会去判断#elif的条件,直到遇到匹配的#endif指令为止。

    5、C#内置委托EventHandler、Func、Action

    EventHandler:

    public delegate void EventHandler(object sender, EventArgs e);

    Func:

    public delegate TResult Func<TResult>();

    public delegate TResult Func<T, TResult>(T arg);

    public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);

    Action:

    public delegate void Action();

    6、获得控制台应用程序的输出

    class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("success1");
                Console.WriteLine("success2");
            }
        }
      private void Start()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo(@"G:VSProjectsGetConsoleGetConsoleinDebugGetConsole.exe");
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError = true;
    
            Process p = Process.Start(startInfo);
            p.WaitForExit();
            string output = p.StandardOutput.ReadToEnd();
            print("输出的成功信息为 = " + output);
            string err = p.StandardError.ReadToEnd();
            print("输出的错误信息为 = " + output);
        }

     ProcessStartInfo startInfo = new ProcessStartInfo(path);
     startInfo.UseShellExecute = false;
     startInfo.RedirectStandardOutput = true;
     startInfo.RedirectStandardError = true;
     startInfo.CreateNoWindow = true;//不显示窗口
     startInfo.Arguments = "123 content abc";//向main函数传参数
  • 相关阅读:
    rgba 和opacity的透明效果有什么不同
    定位 盒模型 浮动 css优先级 伪类伪元素选择器---未完成
    JS中Date对象getYear()方法和getFullYear()方法区别
    Javascript错误提示--SyntaxError: Illegal return statement
    JavaScript高级程序设计-第六章面向对象的程序设计(复习)
    为什么js加事件时不要写括号
    JS函数是如何执行的
    关于JS的总结
    我所知道的数据库6-DDL语言(续)
    我所知道的数据库5-DDL语言
  • 原文地址:https://www.cnblogs.com/MrZivChu/p/xiaozhishi.html
Copyright © 2011-2022 走看看