c#最简单的语句
2013-10-22 22:19:44 a1b2c300 阅读数 341

大半年没写代码了,都忘光了。

今天的项目需要一个简单的分支语句。

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

namespace CaseProject
{
    class Program
    {
        static void Main(string[] args)
        {
            const string myName = "karli";
            const string sexyName = "angeline";
            const string nickName = "ploppy";
            string name;
            Console.WriteLine("What's your name?");
            name = Console.ReadLine(); //从键盘读值都忘了怎么写,my God!
            switch (name.ToLower())
            {
                case myName:
                    Console.WriteLine("You have the same name as me.");
                    break;
                case sexyName:
                    Console.WriteLine("My,what a sexyName you have.");
                    break;
                case nickName:
                    Console.WriteLine("That's a very nick name.");
                    break;
            }
            Console.WriteLine("Hello {0}!",name);
            Console.ReadKey();
        }
    }
}

或者用学习C语言时的习惯写,看的更清楚些,实际上没多大区别。

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

namespace CaseProject
{
    class Program
    {
        static void Main(string[] args)
        {
            const string nameOne="karli", nameTwo="angeline", nameThree="ploppy";
            string name;
            Console.WriteLine("What's your name?");
            name = Console.ReadLine();
            switch (name.ToLower())
            {
                case nameOne:
                    Console.WriteLine("You have the same name as me.");
                    break;
                case nameTwo:
                    Console.WriteLine("My,what a sexyName you have.");
                    break;
                case nameThree:
                    Console.WriteLine("That's a very nick name.");
                    break;
                default:
                    Console.WriteLine("there is no that name.");
                    break;//假如没有写,会报错:c#中控制不能从一个case标签贯穿到另一个case标签。
            }
            Console.WriteLine("Hello {0}!",name);
            Console.ReadKey();
        }
    }
}



2010-11-18 22:33:00 iteye_20954 阅读数 22

image

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

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

//浏览按钮点击事件
private void btnExplorer_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = @"d:/";
ofd.RestoreDirectory = true;//在关闭对话框前是否还原当前目录
ofd.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK) {
txtPath.Text = ofd.FileName;
//创建文件流
FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
//创建读取器,注意此处,如果用 new StreamReader(fs);会出现乱码
StreamReader sr = new StreamReader(fs,System.Text.Encoding.Default);
//读取文件
txtContent.Text = sr.ReadToEnd();
//关闭读取器
sr.Close();
//关闭文件流
fs.Close();
}
}
//写入按钮点击事件
private void btnWrite_Click(object sender, EventArgs e)
{
SaveFileDialog sfd=new SaveFileDialog();
sfd.Filter= "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
DialogResult dr=sfd.ShowDialog();
if(dr!=DialogResult.OK){return;}
FileStream fs = new FileStream(sfd.FileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
sw.Write(txtContent.Text);
sw.Close();
fs.Close();
}
}
}

2016-04-01 15:20:40 qq_15572445 阅读数 243

 (new Thread(() =>
            {
                MessageBox.Show("处理完成");
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                { (sender as Button).IsEnabled = true; }));
            })).Start();

最简单的线程


2016-03-03 21:35:17 qq_15572445 阅读数 216
this.Invoke(new Action(() =>
{
    //这里面写需要进行的委托操作
    //比如线程操作界面的语句等
}));




//举例
this.Invoke(new Action(() =>
{
    this.TextBox1.Text = "12345";
}));


//举例
this.Invoke(new Action(() =>
{
    this.ListBox1.SelectedIndex = 0;
}));
2010-11-18 22:33:00 yenange 阅读数 610

image 

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //浏览按钮点击事件
        private void btnExplorer_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = @"d:/";
            ofd.RestoreDirectory = true;//在关闭对话框前是否还原当前目录
            ofd.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            DialogResult dr = ofd.ShowDialog();
            if (dr == DialogResult.OK) {
                txtPath.Text = ofd.FileName;
                //创建文件流
                FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
                //创建读取器,注意此处,如果用 new StreamReader(fs);会出现乱码
                StreamReader sr = new StreamReader(fs,System.Text.Encoding.Default);
                //读取文件
                txtContent.Text = sr.ReadToEnd();
                //关闭读取器
                sr.Close();
                //关闭文件流
                fs.Close();
            }
        }
        //写入按钮点击事件
        private void btnWrite_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd=new SaveFileDialog();
            sfd.Filter= "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            DialogResult dr=sfd.ShowDialog();
            if(dr!=DialogResult.OK){return;}
            FileStream fs = new FileStream(sfd.FileName, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);
            sw.Write(txtContent.Text);
            sw.Close();
            fs.Close();
        }
    }
}

C#最简单例子

阅读数 18

usingSystem;namespaceConsoleApplication1{classPeople{intage;stringname;publicstringgetname(){name="xiaofang";...

博文 来自: weixin_34320724

最简单的C#快速入门

阅读数 30

在一小时内学会C#。使用例程,简单却完整的探索C#语言的构造和特点。本文特别适合有C++基础却没有太多精力学习C#的读者。关于作者AishaIkram我现在在英国一家软件公司任技术带头人。我是计算机科学的硕士。我主要使用.NET1.1/2.0,C#,VB.NET,ASP.NET,VC++6,MFC,ATL,COM/DCOM,SQL...

博文 来自: weixin_34192993

[C#学习笔记] 5 简单语句与分支语句

阅读数 64

把最后的花括号重新输入可以重新排版!Ctrl+E+D可以重新排版;switch(arg1){  caseconst1:statement1;break;  caseconst2:statement2;break;  ...}  变量除了整型,枚举型还可以用字符串。usingSystem;publicclass***{  publicstaticvoidMain...

博文 来自: CharleeChan

c# 连接ORACLE数据库最精简语句

阅读数 1

stringcosn="DataSource=172.20.65.236;UserId=bjmedicare_qy;Password=bjmedicare_qy";stringsql="select*fromtest_n";OracleConnectionconn=newOracleConnection(cosn);OracleCo...

博文 来自: weixin_34220963

C# switch 语句的简单应用

阅读数 415

publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){inta=Convert.ToInt16(textBox1.Text);intb=0

博文 来自: sangjisuonan
没有更多推荐了,返回首页