.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 = ;
Console.WriteLine("请输入一个数字");
try
{
number = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("输入的内容不能转换为数字");
b = false;
}
if(b)
{
Console.WriteLine(number*);
}
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 % == || year % == && year % != ;
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 = { , , , , , , , , , };
for (int i = ; i < nums.Length - ; i++)
{
for (int j = ; j < nums.Length - - i; j++)
{
if (nums[j] > nums[j + ])
{
int temp = nums[j];
nums[j] = nums[j + ];
nums[j + ] = temp;
}
}
}
for (int i = ; 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 = ; i < adu.Length;i++ )
{
adu[i].say();
}
Console.ReadKey();
}
} public class Aduck
{
public virtual void say()
{
Console.WriteLine("");
}
} public class Bduck : Aduck
{
public override void say()
{
Console.WriteLine("");
}
} public class Cduck : Aduck
{
public override void say()
{
Console.WriteLine("");
}
}
}
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 / )//可以将范围转换成一个定值
{
case ://执行代码一致可省略不写
case : Console.WriteLine("A");
break;
case : Console.WriteLine("B");
break;
case : Console.WriteLine("C");
break;
case : 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("");
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 = ; 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 = ;
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 = ; i < str.Length; i++)
{
Console.WriteLine(str[i]);
}
Console.ReadKey();
} public static void ProStr(string[] str, DelProStr del)
{
for (int i = ; 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>() { , , , , , };
list.RemoveAll(n => n > );
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 = { , , , , , };
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[];
for (int i = ; i < nums.Length; i++)
{
if (del(max, nums[i]) < )
{
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);
}
}
}
.Net 经典案例的更多相关文章
- javascript的理解及经典案例
js的简介: JavaScript是一种能让你的网页更加生动活泼的程式语言,也是目前网页中设计中最容易学又最方便的语言. 你可以利用JavaScript轻易的做出亲切的欢迎讯息.漂亮的数字钟.有广告效 ...
- jQuery基础的工厂函数以及定时器的经典案例
1. jQuery的基本信息: 1.1 定义: jQuery是JavaScript的程序库之一,它是JavaScript对象和实用函数的封装, 1.2 作用: 许多使用JavaScript能实现的交 ...
- Linux运维之道(大量经典案例、问题分析,运维案头书,红帽推荐)
Linux运维之道(大量经典案例.问题分析,运维案头书,红帽推荐) 丁明一 编 ISBN 978-7-121-21877-4 2014年1月出版 定价:69.00元 448页 16开 编辑推荐 1 ...
- 经典案例:那些让人赞不绝口的创新 HTML5 网站
在过去的10年里,网页设计师使用 Flash.JavaScript 或其他复杂的软件和技术来创建网站.但现在你可以前所未有的快速.轻松地设计或创造互动的.有趣好看的网站.如何创建?答案是 HTML5 ...
- Altera OpenCL用于计算机领域的13个经典案例(转)
英文出自:Streamcomputing 转自:http://www.csdn.net/article/2013-10-29/2817319-the-application-areas-opencl- ...
- php中foreach()函数与Array数组经典案例讲解
//php中foreach()函数与Array数组经典案例讲解 function getVal($v) { return $v; //可以加任意检查代码,列入要求$v必须是数字,或过滤非法字符串等.} ...
- 阿里云资深DBA专家罗龙九:云数据库十大经典案例分析【转载】
阿里云资深DBA专家罗龙九:云数据库十大经典案例分析 2016-07-21 06:33 本文已获阿里云授权发布,转载具体要求见文末 摘要:本文根据阿里云资深DBA专家罗龙九在首届阿里巴巴在线峰会的&l ...
- 经典案例之MouseJack
引言:在昨天的文章<无线键鼠监听与劫持>中,我们提到今天会向您介绍一个无线键鼠的监听与劫持的经典案例,<MouseJack>:MouseJack能利用无线鼠标和键盘存在的一些问 ...
- HTML5 CSS3 经典案例:无插件拖拽上传图片 (支持预览与批量) (二)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/31513065 上一篇已经实现了这个项目的整体的HTML和CSS: HTML5 C ...
- Wolsey“强整数规划模型”经典案例之一单源固定费用网络流问题
Wolsey“强整数规划模型”经典案例之一单源固定费用网络流问题 阅读本文可以理解什么是“强”整数规划模型. 单源固定费用网络流问题见文献[1]第13.4.1节(p229-231),是"强整 ...
随机推荐
- 你知道Verilog HDL程序是如何构成的吗
本节通过硬件描述语言Verilog HDL对二十进制编码器的描述,介绍Verilog HDL程序的基本结构及特点. 二十进制编码器及Verilog HDL描述 二十进制编码器是数字电路中常用的电路单元 ...
- nginx_1_初始nginx
一.nginx简介: nginx是一个性能优秀的web服务器,同时还提供反向代理,负载均衡,邮件代理等功能.是俄罗斯人用C语言开发的开源软件. 二.安装nginx step1:安装依赖库 pcre(支 ...
- Java 使用 JDBC 连接数据库的代码整合[MySql、SqlServer、Oracle]-[经过设计模式改造](2020年寒假小目标01)
日期:2020.01.08 博客期:121 星期三 今天对过去整个大二和大三上半学期用到的数据库的方法进行汇总,可以有效的使用.套用,每一个部分都有<软件设计模式>知识,上述代码满足了开闭 ...
- 网易云信-新增自定义消息(iOS版)
https://www.jianshu.com/p/2bfb1c4e9f21 前言 公司业务需要,PC端,移动端都用到了第三方 网易云信 IM来实现在线客服咨询.在这当中难免遇到一些需求是网易云信没有 ...
- OC中四种遍历方式
标准的C语言for循环.Objective-C 1.0出现的NSEnumerator.Objective-C 1.0出现的for in快速遍历.块遍历. 遍历的话,一般是NSArray.NSDicti ...
- 一个Win32程序的进化------转载
一个Win32程序的进化 转载 weixin_34273046 发布于2017-11-15 10:53:00 阅读数 10 收藏 展开 一.为什么要学Win32 要回答这个问题,我们就要先搞清 ...
- ionic3记录之APP运行时网络判断
判断设备网路是否正常: 安装插件: ionic cordova plugin add cordova-plugin-network-information npm install --save@nat ...
- Mac OS 下升级gcc遇到的坑
为了升级gcc,原来版本是4.8,想升级到4.9,因为在Mac上安装cocoNLP一直出错,想试试看是不是gcc版本太低的原因,因此在此想要升级到4.9 经历了三个问题: - brew update太 ...
- zookeeper logs is missing zookeeper 日志丢失
ERROR [main:QuorumPeerMain@85] - Invalid config, exiting abnormally Invalid config, exiting abnormal ...
- class中static总结-静态成员函数和静态成员变量
C++规定const静态类成员可以直接初始化,其他非const的静态类成员需要在类声明以外初始化,我们一般选择在类的实现文件中初始化,初始化的方式是书写一遍类型的定义: //A.cpp ); //使用 ...