[C#]随便记记
匿名数据类型
namespace DEMO { class Program { static void Main(string[] args) { int intValue0 = 1; Console.WriteLine(intValue0.GetType()); var Value1 = 2; Console.WriteLine(Value1.GetType()); var Value2 = 3.14f; Console.WriteLine(Value2.GetType()); var Value3 = "demo"; Console.WriteLine(Value3.GetType()); Value1 = 1.0; //CS0266 } } }
异常处理
using System; namespace ConsoleApp { class TestClass { static void Main(string[] args) { try { Console.WriteLine("Try"); } catch (FormatException e) { Console.WriteLine("Catch"); } finally { Console.WriteLine("Finally"); } Console.ReadKey(); } } }
根据MessageBox不同按钮的点击触发不同的事件
using System; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { DialogResult theDlgResult = MessageBox.Show("提示", "标题", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (theDlgResult == DialogResult.OK) { MessageBox.Show("点击了\"确定\""); } else { MessageBox.Show("点击了\"取消\""); } } private void button2_Click(object sender, EventArgs e) { DialogResult theDlgResult = MessageBox.Show("提示", "标题", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (theDlgResult == DialogResult.Yes) { MessageBox.Show("点击了\"是\""); } else { MessageBox.Show("点击了\"否\""); } } private void button3_Click(object sender, EventArgs e) { DialogResult theDlgResult = MessageBox.Show("提示", "标题", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information); if (theDlgResult == DialogResult.Yes) { MessageBox.Show("点击了\"是\""); } else if (theDlgResult == DialogResult.No) { MessageBox.Show("点击了\"否\""); } else { MessageBox.Show("点击了\"取消\""); } } } }