JAVA到C#.net应该掌握的一些基础语法
来源:互联网 发布:照片大小调整软件 编辑:程序博客网 时间:2024/05/22 03:39
学会了java的开发者,再来学习c#实际上非常简单,在基础语法上没有太多的不同,在你使用c#的时候,你会发现他比java更加灵活
C#中常使用的一些基础函数
1.输出函数,调用之后一闪而过Console.WriteLine("hello");Console.Write("");不换行2.加上这句,不会一闪而过Console.ReadKey();3.从屏幕上读取一个字符串String str = Console.ReadLine();4.占位符输出Console.Write("1+1={0}",2);//跟c语言占位符一样5.把\不当成转义字符String s = @"\\\\"@表示把\不当成转义符6.类型转换String str = "1";int i = Convert.ToInt32(str);//将字符串转换成整数String s = Convert.toString(1);//将数字转换成字符串int.Parse(str); //将字符串转换成数字,失败抛出异常,成功返回数字int.TryParse(str1,out i);//将字符串转换成int 失败返回false7.字符串的截取String str = "abcde";str.subString(2);//返回cdestr.subString(2,5);//返回从2+1开始到第5个组成的字符串 cde7.枚举的使用enum Sex{ MAIL = '男', FEMAIL = '女'}//将char或者int类型转换成枚举char c = '男';Sex sex = (Sex)c;//将String类型转换成枚举String a = "女";Sex sex = (Sex)Enum.Parse(typeof(Sex), a);//sex则为对应男的枚举//将枚举转换成char或者intchar csex = (char)sex.MAIL;//将枚举转换成字符串String ssex = Sex.MAIL.toString();//使用switch,可想将其转换成枚举类型,然后用swith
类和结构体
1.对象的封装using System;namespace HelloWord{ class Person{ private String name; private int age; public String Name{ set{ this.name = value; } get{ return this.name; } } public int Age{ set{ this.age = value; } get{ return this.age; } } } class Program { public static void Main(string[] args) { Person person = new Person(); person.Age = 20; person.Name = "张良"; Console.WriteLine(person.Age); Console.ReadKey(); } }}//get set,还可以写成这样public int Age{ get; set;}//对象的引用Person a = new Person();Person b = a;a.Age ++;Console.WriteLine(b.Age); //此时输出的A.age++后的值,引用类型在传递参数的时候是用指针指向对象 //而 int char datatime bool值类型的赋值的时候是拷贝值,所以两个改变无关同时C#还用到了c中的结构struct Person{ string name; int age;}//在使用的时候有一个好处不需要申明多个类型直接Person person1;person1.name = "张三";person1.age = 1;而不需要定义多个属性
捕获异常信息
public static void Main(string[] args) { Person person = new Person(); person.Age = 20; person.Name = "张良"; try{ person.Age = Convert.ToInt32(Console.ReadLine()); }catch(Exception e){ Console.WriteLine(e.Message.ToString());//打印异常信息 Console.WriteLine(e.Data.ToString()); Console.WriteLine(e.GetType()); //打印异常类型 }finally{ Console.WriteLine("try_catch...执行完毕"); } Console.WriteLine(person.Age); Console.ReadKey(); }除了try cacth 抛出异常用throw new Exception("message");
继承
class Person{
}
class Chinese : Person{
}
//根java不同的是 除了将 extends 写成了 : 其他没什么不一样
常量
常量public const int pi = 3.14;静态变量public static int stage = 10;在静态方法中不能直接调用非静态成员在非静态成员中可以直接调用静态成员在.net中还有静态类用来实现一些纯的函数库,也就是不用直接new的
索引器
索引器就是跟数组一样,方便存取数据用的例String str = "abc";str[0];//直接就可以返回第一个字符下面写一个索引器的实例索引器说白一点就是能够像数组一样方便的操纵数据。.net类库程序随处可见索引器的例子string str ="abc";char c=str[0]; //str[0]就是索引器的例证,有这玩意不是取数据简单方便了?再比如datarow row= datatable.rows[0] //这样取数据行不就方便了?再举个自定义的例子public class IndexerDemo{ string[] strs=new string[3]; public string this[index]{ get{ return strs[index];} set {strs[index]=value;} }}IndexerDemo indexer=new IndexDemo();indexer[0]="a"; //赋值indexer[1]="b";indexer[2]="c";string str =indexer[0];//取值这样操作数据不是很方便吗?自己去实践领悟吧,这东西作用大着点。
另一种返回多个参数的方法
跟java不同的是,java只能返回1个参数而.net可以返回多个参数,只需要在方法中加out就行out必须要赋值public void test(out String name,out int age){ name = "abc"; age = 1;}调用String name;int age;test(out name, out age);-----------------------------------下面具体讲一个例子 /// <summary> /// 判断登录信息 /// </summary> /// <param name="username">用户名</param> /// <param name="password">密码</param> /// <param name="msg">返回的登录信息</param> /// <returns>返回登录状态</returns> public static bool login(String username,String password,out String msg){ if(!username.Equals("admin")){ msg = "账号错误"; return false; } if(!password.Equals("88888888")){ msg = "密码错误"; return false; } msg = "登录成功"; return true; }调用 Console.WriteLine("欢迎来到888挑战杯,请输入账号密码"); String name = Console.ReadLine(); String password = Console.ReadLine(); String msg; login(name,password,out msg); Console.WriteLine(msg);---------------------------模拟TryParse(); /// <summary> /// 模拟TryParse函数,用来将一个字符串转换成一个int /// </summary> /// <param name="str">将转换的字符串</param> /// <param name="result">转换后的int</param> /// <returns>转换成功的状态</returns> public static bool MyTryParse(String str,out int result){ result = 0; try{ result = Convert.ToInt32(result); return true; }catch(Exception e){ return fasle; } }
引用传值
在java中传递一个值参数进去之后,改变其状态,那么他本身是不会改变的int double datatime byte等传递到一个方法中是不会改值的,那么如果使用ref参数,那么相当于指针,我们在里面改变值,那么该值也随着改变在使用ref的时候在方法外部必须赋值下面举一个具体的例子 /// <summary> /// 不使用中间变量,交换两个int类型的变量 /// </summary> /// <param name="a"></param> /// <param name="b"></param> public static void swap(ref int a,ref int b){ a = a - b; b = a + b; a = b - a; }调用 int a = 1; int b = 2; swap(ref a,ref b); Console.WriteLine("{0}and{1}",a,b);输出2and1
winform中常用函数
visible = false 表示隐藏 true 表示可见textBox.appendText(str);//用来给文本框赋值,这样不会产生多个对象textBox.Text = str;//也可以赋值,当文本字符量较多时,建议使用上面Application.Exit();//退出程序str.Lines;//返回用分割换行符所构成的数组//下拉列表Combobox.SelectedIndex; //表示选中的是哪个Combobox.SelectedItem; //表示选中的名称
阅读全文