C#Five
一些小小的程序代码:
namespace 表 { public partial class 时钟 : Form { public 时钟() { InitializeComponent(); } private void textBox1_TextChanged(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { textBox1.Text = DateTime.Now.ToString(); } } }
窗体时钟,点击按钮读取系统时间。
static void Main(string[] args) { while (true) { Console.WriteLine(DateTime.Now.ToString()); System.Threading.Thread.Sleep(1000); Console.Clear(); } }
控制台表,自动读取系统时间,一秒刷新一次并清除页面。
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 x = Convert.ToInt32(Console.ReadLine()); string z = Console.ReadLine(); int y = Convert.ToInt32(Console.ReadLine()); switch (z) { case "+": Console.WriteLine("x加y等于{0}",x+y); break; case "- ": Console.WriteLine("x减y等于{0}", x - y); break; case "*" : Console.WriteLine("x乘y等于{0}", x * y); break; case" / ": Console.WriteLine("x除以y等于{0}",x / y); break; } Console.ReadKey(); } } }
控制台计算机,实现四则运算。
using System; namespace 猜数字 { class Program { static void Main(string[] args) { Console.WriteLine("猜数字1到10之间的整数"); Random r = new Random(); int i = r.Next(10); while (true) { int x = int.Parse(Console.ReadLine()); if (x > i) { Console.WriteLine("大了"); } else if (x < i) { Console.WriteLine("小了"); } else { Console.WriteLine("恭喜你猜对了"); break; } } Console.WriteLine("游戏结束"); Console.ReadKey(); } } }
循环猜数字。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 属性 { class Program { static void Main(string[] args) { anmao dog = new anmao(); dog.name = "xiaobai"; dog.age = 17; Console.WriteLine("名字是:" + dog.name + "年龄是:" + dog.age); Console.ReadKey(); } class anmao { public string name; public int age; } } }
属性基本定义。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 接口 { class Program { static void Main(string[] args) { dog xiaobai = new dog(); cat xiaohua = new cat(); tage xiaohei = new tage(); xiaobai.talk(); xiaohua.talk(); xiaohei.talk(); Console.ReadKey( ); } public interface animal { void talk( ); } public class dog : animal { public void talk() { Console.WriteLine("汪汪汪"); } } public class cat :animal { public void talk( ) { Console.WriteLine("喵喵喵"); } } public class tage { public void talk() { Console.WriteLine("吼吼吼"); } } } }
接口基本定义。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 异常 { class Program { static void Main(string[] args) { try { int a = 0; int b = 5, c; c = b / a; Console.WriteLine(c); } catch { Console.WriteLine("除数不能为0"); } Console.ReadKey(); } } }
异常基本使用。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 指针 { class Program { static void Main(string[] args) { unsafe { int a = 6; int* b = &a; Console.WriteLine(a); Console.WriteLine(*b); Console.WriteLine((int)b); } Console.ReadKey(); } } }
指针
正则表达式。
转义符 .
查找特定元素的目标.
using System.IO; using System.Text; using System; namespace 创建记事本 { class Program { static void Main(string[] args) { // FileStream fs1 = new FileStream("C:\\Users\\Administrator\\Desktop\\及时.txt", FileMode.Create, FileAccess.Write); 新建文本文档 string strTest = "测试";//输入文本 System.IO.File.WriteAllText("C:\\Users\\Administrator\\Desktop\\bug.txt", strTest, Encoding.UTF8);//写入文件夹 按utf8 string text = System.IO.File.ReadAllText("C:\\Users\\Administrator\\Desktop\\bug.txt");//读取文件夹 Console.WriteLine(text);// 打印出文本 Console.ReadKey(); } } }
创建文本文档。
甲之砒霜,彼之蜜糖

浙公网安备 33010602011771号