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);
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/Tanqurey/p/12353348.html