• C#基础02


    1.右键项目目录,属性,可修改程序集名称及命名空间

    这个节约方案中只能有一个Main方法

    using System;
    
    
    namespace Demo
    {
        class Program
        {
            static void Main(string[] args)
            {
                // 使用类
                // Demo.Test.Student
            }
        }
    }

    2.流程控制

    1)switch

    break不可省略

    using System;
    
    
    namespace Demo
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("请录入星期几");
                int weeday = int.Parse(Console.ReadLine());
                switch (weeday)
                {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                        Console.WriteLine("工作日");
                        break;
                    default:
                        Console.WriteLine("休息日");
                        break;
                }
            }
        }
    }

    2) 循环

    while(condition){}

    do{}while(condition)

    for(;;){}

    foreach(var item in arr){}

    foreach不能修改其中数据

    但如果Item为一个对象可以操作其属性

    3)数组

     类型 [] 数组名 = new 类型名[长度]

     类型 [] 数组名 = new 类型名[]{el1,el2,...}

     类型 [] 数组名 = {el1,el2,...}

    .Length 数组长度

    特征:长度固定,下表从0开始,数组中所有数据类型相同

    3.类与对象

    C#中类的成员包括字段,属性,方法

    方法首字母大写

    1)访问修饰符

    public 公开的

    private 只有类内可以使用,默认

    internal 当前项目可用

    示例程序1:

    using System;
    
    
    namespace Demo
    {
        class Program
        {
            static void Main(string[] args)
            {
                Student linda = new Student();
                linda.id = 123;
                linda.name = "Linda";
                linda.SayHi("Tom");
                Console.ReadLine();
            }
        }
        class Student
        {
            // 字段就相当于属性
            public int id;
            public string name;
    
            public void SayHi(string friend)
            {
                Console.WriteLine("{0}你好,我是{1}", friend, this.name);
            }
        }
    }

    2)C#推荐使用属性对字段进行保护

     class Student
        {
            // 字段就相当于属性
            public int id;
            public string name;
            public int age;
    
            //属性,对应age
            public int Age
            {
                set
                {
                    if (value < 0 || value > 125)
                    {
                        Console.WriteLine("data not right");
                    }
                    else
                    {
                        age = value;
                    }
                }
                get
                {
                    return age;
                }
            }
            public void SayHi(string friend)
            {
                Console.WriteLine("{0}你好,我是{1}", friend, name);
            }
        }

    如果不需要对字段进行数据校验,还需要属性吗?

    需要。为了统一性与规范性,字段设为私有不可访问,只能访问属性与方法。

    using System;
    
    
    namespace Demo
    {
        class Program
        {
            static void Main(string[] args)
            {
    
            }
        }
        class Student
        {
            // 字段就相当于属性
            public int id;
            // 不需要校验
            public string name;
            private int age;
    
            //属性,对应age
            public int Age
            {
                private set
                {
                    if (value < 0 || value > 125)
                    {
                        Console.WriteLine("data not right");
                    }
                    else
                    {
                        age = value;
                    }
                }
                get
                {
                    return age;
                }
            }
    
            public string Name { get; set; }
            public void SayHi(string friend)
            {
                this.Age = 20; // 正确,只有类内可以用
                Console.WriteLine("{0}你好,我是{1}", friend, name);
            }
        }
    }

    类的默认修饰符是internal

    using System;
    
    
    namespace Demo
    {
        class Program
        {
            static void Main(string[] args)
            {
    
            }
        }
        class Student
        {
            // 字段就相当于属性
            public int id;
            // 不需要校验
            public string name;
            private int age;
    
            //属性,对应age
            public int Age
            {
                private set
                {
                    if (value < 0 || value > 125)
                    {
                        Console.WriteLine("data not right");
                    }
                    else
                    {
                        age = value;
                    }
                }
                get
                {
                    return age;
                }
            }
    
            public string Name { get; set; }
            public void SayHi(string friend)
            {
                this.Age = 20; // 正确,只有类内可以用
                Console.WriteLine("{0}你好,我是{1}", friend, name);
            }
        }
    }
  • 相关阅读:
    GPS坐标转化距离(短距离模型公式)
    jquery ajax 同步异步的执行
    视频播放的基本原理
    [css或js控制图片自适应]
    asp.net中js和jquery调用ashx的不同方法分享,需要的朋友可以参考一下
    [转载]在网页中插入media,RealPlayer等控件
    数组的几种排序算法的实现(3)
    -- HTML标记大全参考手册[推荐]
    数组的几种排序算法的实现(2)
    数组的几种排序算法的实现(1)
  • 原文地址:https://www.cnblogs.com/Tanqurey/p/12353348.html
Copyright © 2020-2023  润新知