c#学习笔记_结构体

using System;

//计算机程序,执行是以函数作为标签,每一步分别对应一个函数
//如果没有继续触发其他函数,则返回原函数继续下一步

namespace Work12
{

    public struct Person {  //Person是结构体名称
        public string name; //分别代表姓名和年龄的成员变量
        public int age;
        public string identify;//身份证
    }
    public class Function
    {


        //1. 返回值
        //2. 参数
        //3. 函数体(功能代码)
        public static int Avg (int M,int N) {

            int a = 5;

            int c = (M + N) / 2;
            return c;//返回值
        }

        public static void FirstThread () {
            //字符串
            string str = "ab";//为什么字符串可以加
            //            str += 'c';
            //            str += 123;
            //            str += 1.2f;

            //运算符重载
            string str1 = string.Format ("{0}{1}", str, 'c');
            str1 = "abcdfre";
            string str2 = "ab";
            int compare = str1.CompareTo (str2);//用来判定大小,返回1 0 -1
            Console.WriteLine (compare);

            bool have = str1.Contains ("cd");


            //string是不可变类型
            str1 = "abcdef";
            string result = str1.Substring (2);//所有string的运算结果,都需要用返回值接收
            result = result.Remove (3,1);

            result = result.ToUpper ();
            Console.WriteLine (result);

            int a = 3;
            int b = 4;
            int c = Function.Avg (a, b);
            int d = Function.Avg (b, c);

            //将字符串逆序的功能转变为函数

        }


        //静态函数:用来触发流程式调用

        public static void MianThread () {
            Function.FirstThread();

            //声明一个包含手机号、座机号、区号、邮编的字符串数组,将其中手机号挑选出来,并换成
            //"1xx-xxxx-xxxx"的格式

            string[] phones = { "18088888888", "630041", "020", "028", "13800000000" };
            string[] telephones = new string[phones.Length];//建一个新的字符串长度
            int count = 0;
            foreach (string one in phones) {
                if (one.Length == 11) {//是手机号
                    string newStr = one.Insert (3,"-");
                    newStr = newStr.Insert (8, "-");
                    telephones [count] = newStr;
                    count ++;
                }
            }
            Console.WriteLine (telephones [0]);


            //函数与文件

            int [] arr = new int[40];

            Person per1 = new Person ();
            per1.name = "张三";
            per1.age = 20;
            per1.identify = "510001198508201111";


            Person per2 = new Person ();
            per2.name = "李四";
            per2.age = 30;
            per2.identify = "510001198708201111";

            per1.age++;
            per1.name = per2.name;

            //求per1和per2的年龄平方和

            bool isMatch = Function.Match (per1);
            Console.WriteLine (isMatch);

            bool isMatch1 = Function.Match (per2);
            Console.WriteLine (isMatch1);

            Console.WriteLine ("Hello World!");
        }

        //写一个函数,验证身份证号中的出生日期与自身年龄age是否一致
        public static bool Match (Person per) {//传人的信息进去
            string birthday = per.identify.Substring (6,4);
            int year = 2017 - int.Parse (birthday);
            if (year == per.age) {
                return true;
            } 
            return false;
        }

        public Function ()
        {
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值