.NET 经典案例

1、捕捉一只小可爱

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 捕捉一个小可爱
{
    class Program
    {
        static void Main(string[] args)
        {
            bool b = true;
            int number = 0;
            Console.WriteLine("请输入一个数字");
            try
            {
                number = Convert.ToInt32(Console.ReadLine());
            }
            catch
            {
                Console.WriteLine("输入的内容不能转换为数字");
                b = false;
            }
            if(b)
            {
                Console.WriteLine(number*2);
            }
            Console.ReadKey();
        }
    }
}

 

2、判断闰年

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 判断闰年
{
    class Program
    {
        static void Main(string[] args)
        {
            //判断闰年
            Console.WriteLine("请输出需要判断的年份");
            int year = Convert.ToInt32(Console.ReadLine());
            bool b = year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
            Console.WriteLine(b);
            Console.ReadKey();
        }
    }
}

 

3、冒泡排序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 冒泡排序
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
            for (int i = 0; i < nums.Length - 1; i++)
            {
                for (int j = 0; j < nums.Length - 1 - i; j++)
                {
                    if (nums[j] > nums[j + 1])
                    {
                        int temp = nums[j];
                        nums[j] = nums[j + 1];
                        nums[j + 1] = temp;
                    }
                }
            }
            for (int i = 0; i < nums.Length; i++)
            {
                Console.WriteLine(nums[i]);
            }
            Console.ReadKey();
        }
    }
}

 

4、多态

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态
{
    class Program
    {
        static void Main(string[] args)
        {
           Aduck a =  new Aduck();
           Bduck b =  new Bduck();
           Cduck c =  new Cduck();
           Aduck[] adu = {a,b,c};
           for (int i = 0; i < adu.Length;i++ )
           {
               adu[i].say();
           }
           Console.ReadKey();
        }
    }

    public class Aduck
    {
        public virtual void say()
        {
            Console.WriteLine("111");
        }
    }

    public class Bduck : Aduck
    {
        public override void say()
        {
            Console.WriteLine("222");
        }
    }

    public class Cduck : Aduck
    {
        public override void say()
        {
            Console.WriteLine("333");
        }
    }
}

 

5、switch-case

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace switch_case例子
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入一个考试成绩");
            int number = Convert.ToInt32(Console.ReadLine());
            //Console.WriteLine("输入的值/10取整数:{0}",number/10);
            switch (number / 10)//可以将范围转换成一个定值
            {
                case 10://执行代码一致可省略不写
                case 9: Console.WriteLine("A");
                    break;
                case 8: Console.WriteLine("B");
                    break;
                case 7: Console.WriteLine("C");
                    break;
                case 6: Console.WriteLine("D");
                    break;
                default: Console.WriteLine("输入的成绩不及格");
                    break;
            }
            Console.ReadKey();
        }
    }
}

 

6、MD5加密

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace MD5加密
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = GetMD5("123");
            Console.WriteLine(s);
            //double n = 12.345;
            //Console.WriteLine(n.ToString("C"));
            Console.ReadKey();
        }

        public static string GetMD5(string str)
        {
            MD5 md5 = MD5.Create();
            byte[] buffer = Encoding.Default.GetBytes(str);
            byte[] MD5Buffer = md5.ComputeHash(buffer);
            //return Encoding.GetEncoding("GBK").GetString(MD5Buffer);
            string strNew = "";
            for (int i = 0; i < MD5Buffer.Length; i++)
            {
                strNew += MD5Buffer[i].ToString();
            }
            return strNew;
        }
    }
}

 

7、字符串

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 字符串
{
    class Program
    {
        static void Main(string[] args)
        {
            //string s1 = "123";
            //string s2 = "123";
            string s = "a_b c=";
            char[] chr = { ' ', '_', '=' };
            string[] str = s.Split(chr, StringSplitOptions.RemoveEmptyEntries);
            Console.WriteLine(string.Join("", str));
            Console.ReadKey();
        }
    }
}

 

8、模拟磁盘打开文件(面向对象继承)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;

namespace 模拟磁盘打开文件
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请选择要进入的磁盘");
            string path = Console.ReadLine();
            Console.WriteLine("请选择要进入的文件");
            string fileName = Console.ReadLine();
            //文件的全路径:path+fileName
            FileFather ff = GetFile(fileName, path + fileName);
            ff.OpenFile();
            Console.ReadKey();
        }

        public static FileFather GetFile(string fileName, string fullPath)
        {
            string extension = Path.GetExtension(fileName);
            FileFather ff = null;
            switch (extension)
            {
                case ".txt": ff = new TxtPath(fullPath);
                    break;
                case ".jpg": ff = new JpgPath(fullPath);
                    break;
                case ".wav": ff = new WavPath(fullPath);
                    break;
            }

            return ff;
        }
    }

    public abstract class FileFather
    {
        //文件名
        public string FileName
        {
            get;
            set;
        }
        //定义一个构造函数,获取文件全路径
        public FileFather(string fileName)
        {
            this.FileName = fileName;
        }
        //打开文件的抽象方法
        public abstract void OpenFile();
    }

    public class TxtPath : FileFather
    {
        //子类调用父类的构造函数,使用关键字base
        public TxtPath(string fullPath)
            : base(fullPath)
        {

        }

        public override void OpenFile()
        {
            ProcessStartInfo psi = new ProcessStartInfo(this.FileName);
            Process p = new Process();
            p.StartInfo = psi;
            p.Start();
        }
    }

    public class JpgPath : FileFather
    {
        public JpgPath(string fullPath)
            : base(fullPath)
        {

        }

        public override void OpenFile()
        {
            ProcessStartInfo psi = new ProcessStartInfo(this.FileName);
            Process p = new Process();
            p.StartInfo = psi;
            p.Start();
        }
    }

    public class WavPath : FileFather
    {
        public WavPath(string fullPath)
            : base(fullPath)
        {

        }

        public override void OpenFile()
        {
            ProcessStartInfo psi = new ProcessStartInfo(this.FileName);
            Process p = new Process();
            p.StartInfo = psi;
            p.Start();
        }
    }
}

 

 9、序列化和反序列化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace 序列化和反序列化
{
    class Program
    {
        static void Main(string[] args)
        {
            //要将p这个对象传递给电脑
            person p = new person();
            p.Name = "张三";
            p.Gender = "";
            p.Age = 19;
            using (FileStream fswrite = new FileStream(@"C:\Users\CHD\Desktop\粘贴板.txt", FileMode.OpenOrCreate, FileAccess.Write))
            {
                //开始序列化
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fswrite, p);
            }
            Console.WriteLine("序列化成功");
            Console.ReadKey();

            //接收对方发过来的二进制,进行反序列化
            //person p;
            //using (FileStream fsread = new FileStream(@"C:\Users\CHD\Desktop\粘贴板.txt",FileMode.OpenOrCreate,FileAccess.Read))
            //{
            //    BinaryFormatter bf = new BinaryFormatter();
            //    p = (person)bf.Deserialize(fsread);
            //}
            //Console.WriteLine(p.Name);
            //Console.WriteLine(p.Gender);
            //Console.WriteLine(p.Age);
            //Console.ReadKey();
        }
    }

    [Serializable]
    public class person
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _gender;

        public string Gender
        {
            get { return _gender; }
            set { _gender = value; }
        }

        private int _age;

        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }
    }
}

10、为什么要使用委托

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 为什么要使用委托
{
    class Program
    {
        public delegate string DelProStr(string str);

        static void Main(string[] args)
        {
            //三个需求
            //1.将一个字符串数组每个元素都装换成大写
            //2.将一个字符串数组每个元素都装换成小写
            //3.将一个字符串数组每个元素两边都加上 双引号
            string[] str = { "asCDeFg", "HIjklmN", "OpqrsT", "xyZ" };
            //StrToLower(str);
            ProStr(str, delegate(string name)
            {
                return name.ToUpper();
            });

            for (int i = 0; i < str.Length; i++)
            {
                Console.WriteLine(str[i]);
            }
            Console.ReadKey();
        }

        public static void ProStr(string[] str, DelProStr del)
        {
            for (int i = 0; i < str.Length; i++)
            {
                str[i] = del(str[i]);
            }
        }

        //public static string StrToUpper(string str) 
        //{
        //    return str.ToUpper();
        //}

        //public static string StrToLower(string str)
        //{
        //    return str.ToLower();
        //}

        //public static string StrSYH(string str)
        //{
        //    return "\"" + str +"\"";
        //}

        //public static void StrToUpper(string[] str) 
        //{
        //    for (int i = 0; i < str.Length; i++)
        //    {
        //        str[i] = str[i].ToUpper();
        //    }
        //}

        //public static void StrToLower(string[] str) 
        //{
        //    for (int i = 0; i < str.Length; i++)
        //    {
        //        str[i] = str[i].ToLower();
        //    }
        //}

        //public static void StrSYH(string[] str) 
        //{
        //    for (int i = 0; i < str.Length; i++)
        //    {
        //        str[i] = "\"" + str[i] + "\"";
        //    }
        //}
    }
}

11、Lamda表达式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamda表达式
{
    class Program
    {
        public delegate void DelOne();
        public delegate void DelTwo(string name);
        public delegate string DelThree(string name);
        static void Main(string[] args)
        {
            //Lamda表达式  匿名函数的简单写法
            DelOne del = () => { }; //delegate(){};
            DelTwo del2 = (string name) => { };//delegate(string name) { };
            DelThree del3 = (string name) => { return name; };//delegate(string name) { return name; };

            List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6 };
            list.RemoveAll(n => n > 4);
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
}

12、泛型委托

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 泛型委托
{
    public delegate int DelCompare<T>(T t1, T t2);

    class Program
    {
        static void Main(string[] args)
        {
            int[] nums = { 1, 2, 3, 4, 5, 6 };
            int max = GetMax<int>(nums, Compare);
            Console.WriteLine(max);
            string[] names = { "qw", "asd", "zxc", "qsdc", "asdasdd", "qw" };
            string name = GetMax<string>(names, (string s1, string s2) =>
            {
                return s1.Length - s2.Length;
            });
            Console.WriteLine(name);
            Console.ReadKey();
        }

        public static T GetMax<T>(T[] nums, DelCompare<T> del)
        {
            T max = nums[0];
            for (int i = 0; i < nums.Length; i++)
            {
                if (del(max, nums[i]) < 0)
                {
                    max = nums[i];
                }
            }
            return max;
        }

        public static int Compare(int n1, int n2)
        {
            return n1 - n2;
        }
    }
}

13、WinForm窗体传值

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 窗体传值
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(MessMsg);
            f2.Show();
        }

        void MessMsg(string str)
        {
            label1.Text = str;
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 窗体传值
{

    public delegate void DelText(string name);

    public partial class Form2 : Form
    {
        public DelText _del;

        public Form2(DelText del)
        {
            this._del = del;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _del(textBox1.Text);
        }
    }
}

 

转载于:https://www.cnblogs.com/songhe123/p/9713371.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值