zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    }
  • 相关阅读:
    Python Socket 遇到错误: TypeError: a bytes-like object is required, not 'str'
    升级JDK导致无法启动eclipse错误解决办法
    Python安装详细步骤
    如何给电脑配置java环境,以及jdk下载安装
    spring 整合Mybatis 错误:A query was run and no Result Maps were found for the Mapped Statement 'com.xxhh.mapper.XxhhTableBabyCustomMapper.selectAllBaby'. It's likely that neither a Result Type nor a Resul
    spring 整合Mybatis 错误:Error creating bean with name 'loginController': Injection of autowired dependencies failed......xxxxx
    spring 整合Mybatis 《错误集合,总结更新》新手进坑金典新手坑
    xib文件加载研究
    xib文件加载后设置frame无效问题
    xcode中storyboard警告说明
  • 原文地址:https://www.cnblogs.com/Tanqurey/p/12353348.html
Copyright © 2011-2022 走看看