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:\windows\Microsoft.NET\Framework\v3.5下,但是一般在环境变量下设置好了,然后就可以在dos下编译你的文件了!

csc d:\test.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:\VSProjects\GetConsole\GetConsole\bin\Debug\GetConsole.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函数传参数

7、如果在网页源码里看不到对应的网页内容,一般是由于此网页内容是动态创建的,可以通过以下方式进行获取:

F12打开开发者工具 -> 网络 -> 选中Fetch / XHR,详细看这里的网络请求,就知道实际看到的网页内容是从哪个链接里请求的了

posted @ 2015-02-08 22:17  MrZivChu  阅读(345)  评论(0编辑  收藏  举报
分享按钮