推荐文档列表

学习.net心得

时间:2021-12-13 11:15:01 心得体会 我要投稿

学习.net心得

1.命名空间:命名空间是一种特殊的分类机制,它将与特定功能集有关的所有类型都分到一起,是.net避免类

学习.net心得

名冲突的一种方式。

2.变量的作用域:一个变量的作用域是指能够使用该变量的程序区域。for、while或类似语句中声明的局部变

量存在于该循环体内。

(1)字段和局部变量的作用域冲突:

   class Program

   {

    int n=0;//定义一个字段

 static void main(string[] args)

 {

  int n=5;//声明一个局部变量

  Console.WriteLine(n.ToString());//结果输出5

 }

   }

(2)如何引用类级变量:可以把变量声明为静态的,使用类本身来访问,例如:

 static class Process

 {

  static int n=0;

  static void main()

  {

   int n=2;

   Console.WriteLine(Process.n.ToString());//输出结果为0

  }

 }

如果字段不是静态的可以使用this来访问,如下:

 public class Process

 {

  public int n=0;

  static void main()

  {

   int n=2;

   Console.WriteLine(this.n.ToString());//输出结果为0

  }

 }

3.常量的特征:必须在声明时初始化,指定值之后不能再修改;其值必须在编译时用于计算;常量总是静态的

,不允许在常量声明中包含修饰

符static.

4.字符常见的操作:

(1)获取字符串长度和所占字节长度

     string str = "中国";

            Console.WriteLine(str.Length);//输出2

            byte[] bytes = Encoding.Default.GetBytes(str);

            Console.WriteLine(bytes.Length);//输出4

            Console.Read();

(2)查找指定位置是否为空字符:Char.IsWhiteSpace(str,n)

     string str = "中国 人民";

            Console.Write(char.IsWhiteSpace(str, 2));//输出为True

(3)查字符是否是标点符号IsPunctuation('字符');

     string str = "中国 人民,";

            Console.WriteLine(char.IsPunctuation(str, 5));//True

            Console.WriteLine(char.IsPunctuation('A'));//False

            Console.WriteLine(char.IsPunctuation(','));//True

(4)删除字符串最后一个字符的2种方法:

   <1>SubString:

 string str1 = "1,2,3,4,5,";           

 Console.WriteLine(str1.Substring(0, str1.Length - 1));//输出结果1,2,3,4,5

   <2>TrimEnd:

 Console.WriteLine(str1.TrimEnd(','));//输出结果1,2,3,4,5

(5)用字符串分割字符串:

     string str2 = "aaaajsbbbbjsccc";

            string[] sarray = Regex.Split(str2, "js", RegexOptions.IgnoreCase);

            foreach (string s in sarray)

            {

                Console.WriteLine(s);

            }

      最后输出结果为:

   aaaa

   bbbb

   cccc

(6)把字符串123456789转换成12-345-6789的2种方法:

 <1> string a = "123456789";

            a = int.Parse(a).ToString("##-###-####");

            Console.WriteLine(a);//输出12-345-6789

 <2>a=a.Insert(5,"-").Insert(2,"-");

    Console.WriteLine(a);//输出12-345-6789

相关专题:[手机]