C#语言不常用语法笔记

只看过3天C#语法书,了解个大概,与C++等不同之处,或者看开源遇到一些奇异用法,记录一下,脑子不够用的情况下,还是记笔记靠谱。

====================

顺便吐槽下,这年头得会各种编程语言入门级知识,因为指不定哪个语言就有其他语言没实现的经典开源项目。

比如前几天2D平台游戏寻路AI就是AS3的,4,5年没用过了,但是还好是类似C++,Java这种,没复习也能直接看懂。

最烦的是C#这种自创语法,语法糖的,什么Any判断集合非空,{ get => focusOnCamTarget; set => focusOnCamTarget = value; },这种鬼东西,第1次见也不知道是啥,虽然第一感觉像是getter,setter,为啥编译不过也不知道,但是改成老语法

get { return focusOnCamTarget; }
set { focusOnCamTarget = value; }

就行了。

Python平时用的也少,从网上看一般也就是用来抓个网页分析,爬虫,计算统计数学之类的,图像处理,再就是机器学习之类的在用,某些游戏也用来做脚本。

但是谁能想到MakeHuman这种捏人软件会用Python写,有时候真的没办法。

但是这些同JS比起来都好多了,10年前就讨厌JS,恶心的写法,浏览器依赖,以及更恶心的没什么好的调试环境,但是从Gayhub开源来看,JS的干货真的不少,没办法恶心也得学。

感叹下目前的计算机辅助水平以及硬件都太初级了,凡人如我,时不时就要同ASM,C,C++,C#,Java,JS,Python,Matlab做斗争,

期望编程语言也能像软件啊,操作系统之类的有个大一统垄断时代,因为面对陌生的语言,看点东西真心累,简直和外语一样绝望。

====================

函数参数前用this修饰是什么意思?

http://blog.csdn.net/jiankunking/article/details/42749375

防抽,例子

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', '?' },
                             StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }
}

可使用此 using 指令将 WordCount 扩展方法置于范围中:

using ExtensionMethods;

而且,可以使用以下语法从应用程序中调用该扩展方法:

string s = "Hello Extension Methods";
int i = s.WordCount();

完整

using System;

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', '?' },
                             StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }
}

namespace Test
{
    using ExtensionMethods;
    class Program
    {
        static void Main(string[] args)
        {
            string s = "Hello Extension Methods";
            int i = s.WordCount();

            Console.WriteLine(i);//3

        }
    }
}

 ====================

函数明带this[]是什么意思?看起来像个属性索引器,果然就是干这用的。

https://www.cnblogs.com/ArmyShen/archive/2012/08/27/2659405.html

例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace CS_Test
{
    public class SomeClass
    {
        public Dictionary<string, string> dicNames = new Dictionary<string, string>();
        public Dictionary<int, int> dicIDs = new Dictionary<int, int>();

        public string this[string name]
        {
            get { return dicNames[name]; }
            set { dicNames[name] = value; }
        }

        public int this[int id]
        {
            get { return dicIDs[id]; }
            set { dicIDs[id] = value; }
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            SomeClass sc = new SomeClass();
            sc[0] = 233;
            sc[1] = 999;
            sc["Money"] = "钱";
            sc["GirlFriend"] = "RightHand";

            Console.WriteLine(sc[0]);
            Console.WriteLine(sc[1]);
            Console.WriteLine(sc["Money"]);
            Console.WriteLine(sc["GirlFriend"]);
        }
    }
}

  

原文地址:https://www.cnblogs.com/kileyi/p/8135663.html