前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C2第 1 节值类型引用类型介绍和字符串练习

C2第 1 节值类型引用类型介绍和字符串练习

作者头像
静心物语313
发布2020-03-24 11:06:25
4170
发布2020-03-24 11:06:25
举报
代码语言:javascript
复制
 1 第二章第 1 节值类型引用类型介绍和字符串练习
 2 [code]using System;
 3 using System.Collections.Generic;
 4 using System.IO;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace 字符串联系
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             string[] txts = File.ReadAllLines("info.csv", Encoding.Default);
16            
17             //拿到一个字符串数组进行遍历
18             for(int i=0;i<txts.Length;i++)
19             {
20                 //对读取的每一行进行 //去掉多余的符号,比如逗号、双引号//将读取的结果返回到一个数组中去
21                 string[] lines =txts[i].Replace("\"", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
22                 Console.WriteLine("业主的姓名是“{0}”,手机是{1}", lines[0] + lines[1], lines[2]);//lines[0]+lines[1]合并为{0}的占位符
23             }
24             Console.ReadKey();
25         }
26     }
27 }
28 [/code][code]
29 using System;
30 using System.Collections.Generic;
31 using System.Linq;
32 using System.Text;
33 using System.Threading.Tasks;
34 
35 namespace 值类型与引用类型
36 {
37     class Program
38     {
39         static void Main(string[] args)
40         {
41             #region 字符串倒序输出
42 
43 
44             ////接收用户输入的字符串,将其中的字符以与输入相反的顺序输出
45             //Console.WriteLine("请输入要转换的字符串:");
46             //string str = Console.ReadLine();
47             //for (int i = str.Length - 1; i >= 0; i--)
48             //{
49             //    Console.Write(str[i]);//给变输出的方式。就成了按一行输出了
50               
51             //}
52            
53             //// str = getReverse(str);
54           
55             ////Console.WriteLine(str);
56             // Console.ReadKey();
57             #endregion
58             #region //接收用户输入的一句英文,将其中的单词,以反序的方式输出
59             //接收用户输入的一句英文,将其中的单词,以反序的方式输出
60             Console.WriteLine("请输入一句英文:");
61             string str = Console.ReadLine();
62             //I Love You
63             string[] msg = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
64             //     返回值不包括含有空字符串的数组元素
65             for (int i = 0; i < msg.Length; i++)
66             {
67                 msg[i] = getReverse(msg[i]);//对每个单词进行反序 
68                 Console.Write(msg[i] + ' ');
69             }
70             Console.ReadKey(); 
71             #endregion
72         }
73         
74 
75         #region 字符串倒叙输出定义的方法
76         
77         private static string getReverse(string str)
78         {
79             char[] ch = str.ToCharArray(); //字符串转化为一个字符的数组
80             char Temp=' ';//需要加一个空格
81             for(int i=0;i<ch.Length/2;i++)
82             {
83                 Temp=ch[i];
84                 ch[i]=ch[ch.Length-1-i];
85                 ch[ch.Length-1-i]=Temp;
86             }
87             return new string(ch);//将字符数组放进来,就可以得到一个字符串//字符串转化为//一个字符的数组转化为一个字符串;
88 
89         } 
90         //注意注释 *  与 / 之间不能有空格的出现; 
91         #endregion
92         
93     }
94 }
95 
96 [/code]

C2第 2 节: 字符串的练习下

代码语言:javascript
复制
1 C2第 2 节: 字符串的练习下 
 2 [code]using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace 字符串练习2
 9 {
10     class Program
11     {
12 
13         static void Main(string[] args)
14         {
15 
16             #region //练习:123-456---7---89-----123----2把类似的字符串中重复符号”-”去掉,
17             ////既得到123-456-7-89-123-2.
18             //string str = "123-456---7---89-----123----2";
19             //string[] txt = str.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
20             //string newstr = string.Join("-", txt);//字符数字有一个静态的Join方法、、返回值是一个新的字符串
21             //Console.WriteLine(newstr);
22             //Console.ReadKey();
23             #endregion
24              //* 练习:“192.168.10.5[port=21,type=ftp]”,这个字符串表示IP地址为192.168.10.5的服务器的21端口提供的是ftp服务,其中如果“,type=ftp”部分被省略,则默认为http服务。请用程序解析此字符串,然后打印出“IP地址为***的服务器的***端口提供的服务为***”
25             string str = "192.168.10.5[port=21,type=ftp]";
26             string[] msg=str.Split(new string[] { "[port=", "type=", "]" },
27                 StringSplitOptions.RemoveEmptyEntries);
28             Console.WriteLine("Ip地址:{0}",msg[0]);
29             Console.WriteLine("端口号:{0}",msg[1]);
30             Console.WriteLine("协议类型:{0}",msg.Length>2?msg[2]:"http");
31             Console.ReadKey();
32         }
33      
34 
35     }
36 }
37 [/code]

  C2第3节ref和out、装箱与拆箱

C2第3节ref和out、装箱与拆箱 1、使用ref型参数时,传入的参数必须先被初始化。对out而言,必须在方法中对其完成初始化。 2、使用ref和out时,在方法的参数和执行方法时,都要加ref或out关键字。以满足匹配。 3、out适合用在需要retrun多个返回值的地方,而ref则用在需要被调用的方法修改调用者的引用的时候。 装箱拆箱

代码语言:javascript
复制
1 [code]
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace ref与out
 9 {
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 //int n=10;
15 //int result = Show(ref n);//使用ref型参数时,传入的参数必须先被初始化。
16 //Console.WriteLine(result);
17 //Console.ReadKey();
18 int num ;//= 500; //此处的变量num没必要初始化
19 int result = Show(out num);//记得此处需要加关键字 out
20 Console.WriteLine(result);
21 Console.ReadKey();
22 }
23 ////ref型参数可进可出方法,但在进入前需要先初始化
24 //public static int Show(ref int num)
25 //{
26 // return num + 1000;
27 //}
28 ////out型参数只出不进(可以进)方法,但是在方法里边需要先初始化
29 public static int Show(out int num)//错误    1    无法定义重载方法“Show”,因为它与其他方法仅在 ref 和 out 上有差别//说明ref与out是等价的关系、、ref与out只有一个存在
30 {
31 num = 100;//此处num前边不需要加关键字 int num = 100;
32 return num + 123;//错误    1    当前上下文中不存在名称“num”//需要先对out值在方法中初始化
33 
34 }
35 }
36 }
37 [/code]
38 
39 [code]
40 using System;
41 using System.Collections.Generic;
42 using System.Linq;
43 using System.Text;
44 using System.Threading.Tasks;
45 
46 namespace 装箱与拆箱
47 {
48 class Program
49 {
50 static void Main(string[] args)
51 {
52 int num = 100;
53 object obj = num;//装箱
54 //值类型隐式的转换为引用类型--装箱
55 
56 double db = (object)obj;//拆箱但是失败了,装箱是什么类型的值就读取什么类型的值
57 
58 }
59 }
60 }
61 [/code] 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档