当前位置: 移动技术网 > IT编程>开发语言>.net > C#知识拾遗

C#知识拾遗

2019年01月31日  | 移动技术网IT编程  | 我要评论

音乐银行2014,写手,血鳞鲶鱼

 参数验证方式

1.    一般方法

1.1 手动验证

最为普遍常见,略。

1.2 使用扩展方法验证

在c#3.0 中,引入了扩展方法,可以以一种更优雅的方式来进行参数验证,如:

 1 //参数辅助类
 2 public static class argumenthelper
 3 {
 4     public static void requirerange(this int value, int minvalue, int maxvalue, string argumentname)
 5     {
 6         if (value > maxvalue || value < minvalue)
 7         {
 8             throw new argumentexception(string.format("the value must be between {0} and {1}", minvalue, maxvalue), argumentname);
 9         }
10     }
11 
12     public static void requirenotnullorempty(this string value, string argumentname)
13     {
14         if (string.isnullorempty(value))
15         {
16             throw new argumentexception("the value can't be null or empty", argumentname);
17         }
18     }
19 }
20 
21 //用户注册
22 public bool register(string name, int age)
23 {
24    name.requirenotnullorempty("name");
25    age.requirerange(10,70,"age");
26     //insert into db
27 }
view code

2. 使用类型或框架

使用类库

如,,(validation application block组件),(.net 4.0正式引用)等。

2.2 使用框架

         如asp.net mvc的model中数据标记(data annotations) 属性。

 

参考资料:c# 中参数验证方式的演变

 

全角与半角字符转换

全角空格为12288,半角空格为32,其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248。基于此便不难实现他们之间的转换:

 1         /// <summary>
 2         ///转全角的函数(sbc case)
 3         ///全角空格为12288,半角空格为32
 4         ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
 5         /// </summary>
 6         /// <param name="input">输入</param>
 7         /// <returns></returns>
 8         public static string tosbc(string input)
 9         {
10             char[] c = input.tochararray();
11 
12             for (int i = 0; i < c.length; i++)
13             {
14                 if (c[i] == 32)
15                 {
16                     c[i] = (char)12288;
17 
18                     continue;
19                 }
20 
21                 if (c[i] < 127)
22 
23                     c[i] = (char)(c[i] + 65248);
24             }
25 
26             return new string(c);
27         }
28 
29 
30         /// <summary>
31         ///转半角的函数(dbc case)
32         ///全角空格为12288,半角空格为32
33         ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
34         /// </summary>
35         /// <param name="input"></param>
36         /// <returns></returns>
37         public static string todbc(string input)
38         {
39             char[] c = input.tochararray();
40 
41             for (int i = 0; i < c.length; i++)
42             {
43                 if (c[i] == 12288)
44                 {
45                     c[i] = (char)32;
46                     continue;
47                 }
48 
49                 if (c[i] > 65280 && c[i] < 65375)
50 
51                     c[i] = (char)(c[i] - 65248);
52             }
53 
54             return new string(c);
55         }
view code

参考资料:c#全角和半角转换

 

转string时,null的处理:

convert.tostring能处理字符串为null的情况。tostring方法不能处理字符串为null的情况,会抛出异常。

 

string/byte[]/memorystream/base64string的相互转换

关系如下图:

1.字符串转比特数组

(1)byte[] bt=system.text.encoding.default.getbytes("字符串");

(2)byte[] bt=convert.frombase64string("字符串");

2.字符串转流

(1)memorystream ms=new memorystream(system.text.encoding.default.getbytes("字符串"));

(2)memorystream ms=new memorystream(convert.frombase64string("字符串"));

3.流转比特数组

(1)byte[] bt=ms.toarray();

(2)memorystream ms=new memorystream();ms.write(bt,0,ms.length);

4.流转字符串

(1)string str=convert.tobase64string(ms.toarray());

(2)string str=system.text.encoding.default.getstring(ms.toarray());

5.比特数组转字符串

(1)string str=system.text.encoding.default.getstring(bt);

(2)string str=convert.tobase64string(bt);

6.比特数组转流

(1)memorystream ms=new memorystream(bt);

(2)memorystream ms=new memorystream();ms.read(bt,0,bt.lenght);

 

参考资料:字符串string 、byte[]、memorystream、base64string的相互转换

 

c# 反射invoke调用方法获得out带出的值

         输出参数在最后,基于些获取方法如下:

1             var msg = string.empty;
2             object[] parameters = new object[] { model, null };
3 
4             //函数签名为public bool update(db_bedinfo model, out string mess);
5             updatemethod.invoke(serviceobj, parameters);
6 
7             //parameters[1]即为out带出的值
8             msg = convert.tostring(parameters[1]);

 

具名参数和可选参数

  具名参数 和 可选参数 是 c#  4.0 出来的新特性。当有多个可选参数,前面的可选参数不想传时,可使用具名参数解决。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网