C#基础

字节跳动开源OpenDay  – 百格活动 (bagevent.com)

SiKi学院|SiKi学堂 – unity|u3d|虚幻|ue4/5|java|python|人工智能|视频教程|在线课程 (sikiedu.com)

SmartBest-Yang/CSharp_Demo at master (github.com)

枚举

enum Gender
{
MALE,
FEMALE
}
Gender gender = Gender.MALE;

应用场景:游戏中的各种状态(游戏中、暂停、在线、离线)月份(Jan,Feb…)UI(MenuUI……)

结构体

// See https://aka.ms/new-console-template for more information

using TestProject;

namespace Test
{
    class Program
    {
        struct Person
        {
            public int Id;
            public string Name;
            public string gender;
        }

        static void Main(string[] args)
        {
            Person person = new Person();
            person.Id = 1;
            person.Name = "Test";
            person.gender = "Male";

        }
    }
}

结构体函数

// See https://aka.ms/new-console-template for more information

using TestProject;

namespace Test
{
    class Program
    {
        struct Person
        {
            public int Id;
            public string Name;
            public string gender;
            public void Print()
            {
                Console.WriteLine(Id);
                Console.WriteLine(Name);
                Console.WriteLine(gender);
            }
        }

        static void Main(string[] args)
        {
            Person person = new Person();
            person.Id = 1;
            person.Name = "Test";
            person.gender = "Male";
            person.Print();
        }
    }
}

委托

委托即函数的引用

// See https://aka.ms/new-console-template for more information

using TestProject;

namespace Test
{
    class Program
    {
        public static int Add(int a, int b)
        {
            return a + b;
        }

        public static int Subtract(int a, int b)
        {
            return a - b;
        }

        // 返回值,参数列表一致
        delegate int Action(int a, int b);

        static void Main(string[] args)
        {
            // 委托
            Action action;
            action = Add;
            Console.WriteLine(action(1,2));
            action = Subtract;
            Console.WriteLine(action(6,3));

        }
    }
}

委托的应用

// See https://aka.ms/new-console-template for more information

using TestProject;

namespace Test
{
    class Program
    {
        delegate void onDieDelegate();
        static void Play(onDieDelegate onDie)
        {
            Console.WriteLine("Operation 1");
            Console.WriteLine("Operation 2");
            Console.WriteLine("Operation 3");
            // 不直接指定调用哪个方法
            if (onDie != null)
            {
                onDie();
            }
        }
        static void ShowUIA()
        {
            Console.WriteLine("Show UI A");
        }

        static void ShowUIB()
        {
            Console.WriteLine("Show UI B");
        }

        static void Main(string[] args)
        {
            Play(ShowUIA);
            // Play(ShowUIB);
        }

    }
}

在一个固定流程后,可以指定调用什么方法

封装及访问修饰符

  • public:所有对象都可以访问;
  • internal:同一个程序集的对象可以访问;
  • protected:只有该类对象及其子类对象可以访问
  • protected internal:访问限于当前程序集或派生自包含类的类型。
  • private:对象本身在对象内部可以访问;

internal测试

namespace MemberAccess
{
    class Person
    {
        internal int age;
        internal int height;
        internal int width;

        // 没有指定访问修饰符,类成员的默认访问修饰符 private。
        int Result()
        {
            return height * width;
        }
        public void Display()
        {
            Console.WriteLine(Result());
        }
    }

    class ExecuteClass
    {
        static void Main(string[] args)
        {
            Person person = new Person();
            person.height = 10;
            person.width = 2;
            person.Display();
        }
    }
}

C#可空类型(Nullable)

?和??

? 单问号用于对 int、double、bool 等无法直接赋值为 null 的数据类型进行 null 的赋值,意思是这个数据类型是 Nullable 类型的。

int? i = 4;
// 等同于
Nullable<int> i = new Nullable<int>(4);

int b;  // 默认为0
int? c; // 默认为null

?? 双问号用于判断一个变量在为 null 的时候返回一个指定的值。

可空类型Nullable

可空类型可以表示其基础值类型正常范围内的值,再加上一个 null 值。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇