C#(一) helloworld
来源:互联网 发布:淘宝买东西优惠软件 编辑:程序博客网 时间:2024/05/18 02:24
1.简单helloworld
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { //Console.Beep(); Console.WriteLine("Hello ,world"); //换行 //相当于java里面的 //System.out.println("Hello,world); Console.Write("aaa"); //不换行 //System.out.print("aaa"); Console.ReadKey(); } }}
有时我们只想看看有没有语法错误,可以执行生成解决方案。
vs2013显示行号:设置字体
工具—》环境—》字体和颜色
工具—》文本编辑器—》C# :行号
或者工具—》文本编辑器—》所有语言
2.一个解决方案里面新建多个项目
2.1 新建—》项目—》在解决方法的地方,选择添加到解决方案
2.2或者在右击”解决方案XX”,如下图所示:
2.3 想运行解决方案里面的哪个项目,右击该项目,设置为启动项目
2.4 设置为启动的项目的颜色为加粗黑
2.5 把当前编辑的项目设置为启动项目;
右击“解决方案xxx”,—-》属性,–》启动项目–》当前选定内容。
3.注释C#中有三种注释
3.1 单行注释 //
3.2 多行注释/* aaaa **/
3.3文档注释 在类或方法的前面 ///
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication1{ /// <summary> /// 文档注释 /// </summary> class Program { /// <summary> /// 这是对方法的注释 /// </summary> /// <param name="args"></param> static void Main(string[] args) { //Console.Beep(); Console.WriteLine("Hello ,world"); Console.WriteLine("你好"); /* Console.Write("aaa"); Console.Write("bbb"); **/ Console.ReadKey(); } }}
4 折叠代码
region #endregion
#region AAAAAAA Console.WriteLine("******************************************"); Console.WriteLine("* 这是我的第一个C#程序 *"); Console.WriteLine("******************************************"); #endregion
5.占位符的使用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication5{ class Program { public Program() { } static void Main(string[] args) { String name = "张三"; char sex = '男'; int age = 22; Console.WriteLine("我的名字是{0},性别:{1},年龄:{2}",name,sex,age); Console.ReadKey( ); } }}
6.枚举的使用
作用:1.限制用户不能随意赋值,只能在定义枚举的值中选择。值不能是int类型,敲一下空格就可以把枚举的提示弹出。枚举类型的值默认有个编号,从0开始
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication4{ enum Gender { 男 , 女 } enum data { 星期一, 星期二, 星期三, 星期四, 星期五, 星期六, 星期天 } class Program { static void Main(string[] args) { Gender sex; sex = Gender.男; Console.ReadLine((int)sex); //输出0 sex= Gender.女; Console.ReadLine((int)sex);//输出1 switch (sex) { case Gender.男: Console.WriteLine("男性"); break; case Gender.女: Console.WriteLine("女性"); break; } Console.ReadKey(); } }}
注意下面的写法:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication4{ enum Gender { 男=1, 女 }; enum Data { 星期一, 星期二, 星期三, 星期四, 星期五, 星期六, 星期天 } class Program { static void Main(string[] args) { Gender sex; sex = Gender.男; Console.WriteLine((int)Gender.男);//1 Console.WriteLine((int)Gender.女);//2 Console.WriteLine((int)Data.星期二); //1 Console.ReadKey(); } }}
枚举的转换:字符串转换成枚举
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication4{ enum Gender { 男=1, 女 }; enum Data { 星期一, 星期二, 星期三, 星期四, 星期五, 星期六, 星期天 } class Program { static void Main(string[] args) { Gender sex; Console.WriteLine("请输入你的性别?"); string s = Console.ReadLine(); try { sex = ( Gender ) ( Enum.Parse ( typeof ( Gender ) , s ) ); Console.WriteLine ( "你输入的性别为:" + sex); } catch { Console.WriteLine ( "你输入的性别有误" ); } Console.ReadKey(); } }}
7 结构体的使用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication1{ enum Gender { 男 , 女 } public struct Person { public string name; public int age; public Gender sex; public void run ( ) { } } class Program { static void Main ( string[] args ) { Person person; person.name = "张三"; person.age=18; person.sex = Gender.男; Person person2; person2.name = "李四"; person2.age = 20; person2.sex = Gender.女; } public void todo(){ } public int add ( int a , int b ) { return a + b ; } public static void togo ( ) { } }}
修饰符 struts 名称 {
属性;
方法;
}
类的前身就是结构体。
0 0
- C#(一) helloworld
- C++(一)——HelloWorld
- C 语言现代编程一 HelloWorld
- 从C看C++之(一)HelloWorld
- HelloWorld.c
- [c/c++]helloWorld
- 初学Java,HelloWorld(一)
- servlet helloworld(一)
- Hibernate HelloWorld(一)
- Spring MVC (一) - HelloWorld
- SpringMVC入门一:helloWorld
- JNI学习(一)----helloworld
- 初学Java,HelloWorld(一)
- wayland helloworld (一)
- Akka 【一】 HelloWorld
- struts2 学习一 HelloWorld
- 实例一:HelloWorld
- xfire基础一: helloworld
- 三维旋转:旋转矩阵,欧拉角,四元数
- 1022. Digital Library (30) -map -字符串处理
- Android Stdio 引入自定义framework.jar 自定义framework-res.apk 中 attr
- 安卓开发打开系统下载管理历史.
- <论语.八佾>之绘事后素
- C#(一) helloworld
- Android中Gallery和ImageSwitcher的使用
- SQLite数据库操作
- 那个和你一起吃路边摊的姑娘为什么没有陪你走到最后
- iOS多线程编程NSThread的使用
- 3.2-6
- Hibernate的主键声称策略
- Eclipse中设置花括号对齐到下一行的方法
- Eclipse中使用Maven常用的依赖以及错误