频道栏目
首页 > 资讯 > C# > 正文

JAVA到C#.net之C#中常使用的一些基础函数

17-12-26        来源:[db:作者]  
收藏   我要投稿

学会了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 失败返回false

7.字符串的截取
String str = "abcde";
str.subString(2);//返回cde
str.subString(2,5);//返回从2+1开始到第5个组成的字符串 cde

7.枚举的使用
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或者int
char 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);

-----------------------------------下面具体讲一个例子

        /// 
        /// 判断登录信息
        /// 
        ///用户名
        ///密码
        ///返回的登录信息
        /// 返回登录状态
        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();

        /// 
        /// 模拟TryParse函数,用来将一个字符串转换成一个int
        /// 
        ///将转换的字符串
        ///转换后的int
        /// 转换成功的状态
        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的时候在方法外部必须赋值

下面举一个具体的例子

        /// 
        /// 不使用中间变量,交换两个int类型的变量
        /// 
        ///
        ///
        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;  //表示选中的名称
相关TAG标签
上一篇:[Good]类的继承和多态~~~
下一篇:封装Ajax代码实例
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站