给定N个整数,请使用冒泡算法按照从大到小的顺序排序 1.可对N个整数的某一段(连续的M整数)排序 2.要求具有一定的可测试性 3.C#语言 ——————-- 思路: 1.冒泡算法 2.针对部分排序 3.可测试性 先上一段最简单的代码实现冒泡算法--这里错误的使用了选择排序,请参看改进版本的修正 int[] list= new int[] {1, 2, 3, 4, 5}; for (int i=0;i<list.Length;i++) for (int j=i+1;j<list.Length;j++) { if (list[i]<list[j]) { int tmp=list[i]; list[i]=list[j]; list[j]=tmp; } } for (int i=0;i<list.Length;i++) System.Console.Write("{0}\t",list[i]); System.Console.WriteLine(""); 观看上述代码,有如下发现 1)行1,主要进行数据的初始化工作,准备数据 2)行2-11,主要实现冒泡排序算法 3)行12-14,主要是显示结果 4)行1-14,包含算法实现,调用,实现和使用糅合在一起 第一次改进: 1)数据初始化部分,其实属于调用部分,此时用的是数组,扩展性较差,这里改成List<int>,并将此步重构成函数 //初始化N个数据void Init(List<int> list,int count){ System.Random a=new Random(); for (int i=0;i<count;i++) list.Add(a.Next(100));} 这里初始化数据,主要是减少人工干预,自动产生测试数据,实际生产过程中,是需要按照实际情况,选取一些比较特殊的数据作为测试数据的. 2)冒泡排序算法实现过程部分,也可以重构成函数 //实现冒泡算法——这里错误的使用了选择排序 void Bubble(List<int> list) { for (int i=0;i<list.Count;i++) for (int j=i+1;j<list.Count;j++) { if (list[i]<list[j]) { int tmp=list[i]; list[i]=list[j]; list[j]=tmp; } } } 正确的冒泡排序为 void Bubble(List<int> list) { bool bFlag=false; for (int i=0;i<list.Count;i++) { bFlag=false; for(int j=list.Count-1-1 ;j>i-1;j--) { if (list[j]<list[j+1]) { int tmp=list[j+1]; list[j+1]=list[j]; list[j]=tmp; bFlag=true; } } if (!bFlag) break; } } 将排序的代码,重构成函数,使得算法可以非常容易进行测试,只需要将精心设计的测试数据传给函数,就可以了 3)显示结果,也是重构成函数 //显示结果 void Print(List<int> list) { for (int i=0;i<list.Count;i++) System.Console.Write("{0}\t",list[i]); System.Console.WriteLine(""); } 4)最终调用过程 public static void Main(){ List<int> list=new List<int>(); //产生测试数据 Init(list,8); //打印测试数据 Print(list); //按照从大到小的顺序排序 Bubble(list); //打印排序后的结果 Print(list); } 第二次改进: 第一次改进中,基本解决冒泡算法和可测试性的问题,但是还有一个重要问题没有解决,就是针对N个整数中的某一段连续M个数据进行排序,所以这次的改进主要集中在<冒泡排序算法实现>函数的改进 很明显,要实现这个功能,只需要,在 Bubble这个函数增加两个参数,标识出M的上下限,Bubble变成如下形式
1 |
<span style="color:#0000FF;">void</span> Bubble(List<<span style="color:#0000FF;">int</span>> list,<span style="color:#0000FF;">int</span> low,<span style="color:#0000FF;">int</span> high) |
新的实现(注意,这里我的冒泡算法的实现是不对的,我用的是选择排序,经过一个园子里的兄弟提醒,我查过资料,发现的确用错了) 选择排序(Selection sort) 首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。 void Bubble(List<int> list,int low,int high){ int iHigh= list.Count<high+1? list.Count : high+1 ; int iLow=low<0? 0 :low ; //System.Console.WriteLine("{0}\t{1}",iLow,iHigh); for (int i=iLow;i<iHigh;i++) for (int j=i+1;j<iHigh;j++) { if (list[i]<list[j])//比较不一定相邻 { int tmp=list[i]; list[i]=list[j]; list[j]=tmp; } } } 下面是更正后的冒泡排序代码 冒泡排序(BubbleSort) 依次比较相邻的两个数,将小数放在前面,大数放在后面。 static void Bubble(List<int> list,int low,int high) { int iHigh= list.Count<high+1? list.Count : high+1 ; int iLow=low<0? 0 :low ; bool bFlag=false; for (int i=iLow;i<iHigh;i++) { bFlag=false; for(int j=iHigh-1-1 ;j>i-1;j--) { if (list[j]<list[j+1])//比较相邻 { int tmp=list[j+1]; list[j+1]=list[j]; list[j]=tmp; bFlag=true; } } if (!bFlag) break; } } 并提供一个重载函数
1 |
<span style="color:#0000FF;">void</span> Bubble(List<<span style="color:#0000FF;">int</span>><span style="color:#000000;"> list)<br /></span><span style="color:#000000;">{ <span style="color:#008080;"> <br /></span></span>Bubble(list,<span style="color:#800080;">0</span><span style="color:#000000;">,list.Count-1);<br /></span>} |
调用: public static void Main() { List<int> list=new List<int>(); //产生测试数据 Init(list,8); //打印测试数据 Print(list); //按照从大到小的顺序排序,针对序号2-5的之间的数据 Bubble(list,2,5); //打印排序后的结果 Print(list); } 至此,题目要求的目的全部达到,不过还是少了点什么,下面进行第三次改进 第三次改进: 第一次改进和第二次改进的结果,还是采用面向过程的方法,第三次改进侧重与面向对象的方法,就是封装 三个主要函数中都有List<int> list参数,这个是主要数据,我们用类来封装它,如下给出完整代码 public class BubbleSort { List<int> _list; public BubbleSort() { _list=new List<int>(); } public BubbleSort(List<int> list) { _list=list; } public void Sort() { Sort( _list,0,_list.Count-1); } public void Sort(int low,int high) { Sort( _list,low,high); } //实现冒泡算法--这里错误使用选择排序,请替换为第二次改进中的正确实现 public void Sort(List<int> list,int low,int high) { //int iHigh= list.Count<low+count? list.Count : high ; int iHigh= list.Count<high+1? list.Count : high+1 ; int iLow=low<0? 0 :low ; //System.Console.WriteLine("{0}\t{1}",iLow,iHigh); for (int i=iLow;i<iHigh;i++) for (int j=i+1;j<iHigh;j++) { if (list[i]<list[j]) { int tmp=list[i]; list[i]=list[j]; list[j]=tmp; } } } //初始化N个数据 public void Init(int count) { _list.Clear(); System.Random a=new Random(); for (int i=0;i<count;i++) _list.Add(a.Next(100)); } //显示结果 public void Print(List<int> list) { Print(list,0,list.Count-1,true); } public void Print(List<int> list,int low,int high,bool IsNewLine) { int iHigh= list.Count<high+1? list.Count : high+1 ; int iLow=low<0? 0 :low ; for (int i=iLow;i<iHigh;i++) System.Console.Write("{0}\t",list[i]); if (IsNewLine) System.Console.WriteLine(""); } public void Print(int low,int high,bool IsNewLine) { Print(_list,low,high,IsNewLine); } //将排序的M个数据用红色显示 public void Print(int low,int high) { Print(0,low-1,false); System.Console.ForegroundColor=ConsoleColor.Red; Print(low,high,false); System.Console.ResetColor(); Print(high+1,_list.Count,true); } public void Print() { Print(_list); } //for test public void Test() { //产生测试数据 Init(10); //打印测试数据 Print(); //按照从大到小的顺序排序 int[] iLowHigh=new int[]{4,7}; Sort(iLowHigh[0],iLowHigh[1]); //Sort(-1,8); //Sort(0,18); //Sort(-1,18); //打印排序后的结果 //Print(); Print(iLowHigh[0],iLowHigh[1]); } } 调用代码:
1 |
<span style="color:#008080;">1</span> BubbleSort bs=<span style="color:#0000FF;">new</span><span style="color:#000000;"> BubbleSort();<br /></span><span style="color:#008080;">2</span> bs.Test(); |
View Details
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
using System; using System.Configuration.Install; using System.Collections; using System.Collections.Specialized; IDictionary stateSaver = new Hashtable(); 一、安装服务: private void InstallService(IDictionary stateSaver, string filepath) { try { System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController("ServiceName"); if(!ServiceIsExisted("ServiceName")) { //Install Service AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller(); myAssemblyInstaller.UseNewContext = true; myAssemblyInstaller.Path =filepath; myAssemblyInstaller.Install(stateSaver); myAssemblyInstaller.Commit(stateSaver); myAssemblyInstaller.Dispose(); //--Start Service service.Start(); } else { if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending) { service.Start(); } } } catch (Exception ex) { throw new Exception("installServiceError\n" + ex.Message); } } 或者 /// <summary> /// 安装服务 /// </summary> /// <param name="p_Path">指定服务文件路径</param> /// <param name="p_ServiceName">返回安装完成后的服务名</param> /// <returns>安装信息 正确安装返回""</returns> public static string InsertService(string p_Path, ref string p_ServiceName) { if (!File.Exists(p_Path)) return "文件不存在!"; p_ServiceName = ""; FileInfo _InsertFile = new FileInfo(p_Path); IDictionary _SavedState = new Hashtable(); try { //加载一个程序集,并运行其中的所有安装程序。 AssemblyInstaller _AssemblyInstaller = new AssemblyInstaller(p_Path, new string[] { "/LogFile=" + _InsertFile.DirectoryName + "\\" + _InsertFile.Name.Substring(0, _InsertFile.Name.Length - _InsertFile.Extension.Length) + ".log" }); _AssemblyInstaller.UseNewContext = true; _AssemblyInstaller.Install(_SavedState); _AssemblyInstaller.Commit(_SavedState); Type[] _TypeList = _AssemblyInstaller.Assembly.GetTypes();//获取安装程序集类型集合 for (int i = 0; i != _TypeList.Length; i++) { if (_TypeList[i].BaseType.FullName == "System.Configuration.Install.Installer") { //找到System.Configuration.Install.Installer 类型 object _InsertObject = System.Activator.CreateInstance(_TypeList[i]);//创建类型实列 FieldInfo[] _FieldList = _TypeList[i].GetFields(BindingFlags.NonPublic | BindingFlags.Instance); for (int z = 0; z != _FieldList.Length; z++) { if (_FieldList[z].FieldType.FullName == "System.ServiceProcess.ServiceInstaller") { object _ServiceInsert = _FieldList[z].GetValue(_InsertObject); if (_ServiceInsert != null) { p_ServiceName = ((ServiceInstaller)_ServiceInsert).ServiceName; return ""; } } } } } return ""; } catch (Exception ex) { return ex.Message; } } 二、卸载windows服务: private void UnInstallService(string filepath) { try { if (ServiceIsExisted("ServiceName")) { //UnInstall Service AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller(); myAssemblyInstaller.UseNewContext = true; myAssemblyInstaller.Path = filepath; myAssemblyInstaller.Uninstall(null); myAssemblyInstaller.Dispose(); } } catch (Exception ex) { throw new Exception("unInstallServiceError\n" + ex.Message); } } 三、判断window服务是否存在: private bool ServiceIsExisted(string serviceName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController s in services) { if (s.ServiceName == serviceName) { return true; } } return false; } 四、启动服务: private void StartService(string serviceName) { if (ServiceIsExisted(serviceName)) { System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName); if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending) { service.Start(); for (int i = 0; i < 60; i++) { service.Refresh(); System.Threading.Thread.Sleep(1000); if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running) { break; } if (i == 59) { throw new Exception(startServiceError.Replace("$s$", serviceName)); } } } } } 另外方法 /// <summary> /// 启动服务 /// </summary> /// <param name="serviceName"></param> /// <returns></returns> public static bool ServiceStart(string serviceName) { try { ServiceController service = new ServiceController(serviceName); if (service.Status == ServiceControllerStatus.Running) { return true; } else { TimeSpan timeout = TimeSpan.FromMilliseconds(1000 * 10); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } } catch { return false; } return true; } 五、停止服务: private void StopService(string serviceName) { if (ServiceIsExisted(serviceName)) { System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName); if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running) { service.Stop(); for (int i = 0; i < 60; i++) { service.Refresh(); System.Threading.Thread.Sleep(1000); if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped) { break; } if (i == 59) { throw new Exception(stopServiceError.Replace("$s$", serviceName)); } } } } } 另外一方法: /// <summary> /// 停止服务 /// </summary> /// <param name="serviseName"></param> /// <returns></returns> public static bool ServiceStop(string serviseName) { try { ServiceController service = new ServiceController(serviseName); if (service.Status == ServiceControllerStatus.Stopped) { return true; } else { TimeSpan timeout = TimeSpan.FromMilliseconds(1000 * 10); service.Stop(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } } catch { return false; } return true; } 六 修改服务的启动项 /// <summary> /// 修改服务的启动项 2为自动,3为手动 /// </summary> /// <param name="startType"></param> /// <param name="serviceName"></param> /// <returns></returns> public static bool ChangeServiceStartType(int startType, string serviceName) { try { RegistryKey regist = Registry.LocalMachine; RegistryKey sysReg = regist.OpenSubKey("SYSTEM"); RegistryKey currentControlSet = sysReg.OpenSubKey("CurrentControlSet"); RegistryKey services = currentControlSet.OpenSubKey("Services"); RegistryKey servicesName = services.OpenSubKey(serviceName, true); servicesName.SetValue("Start", startType); } catch (Exception ex) { return false; } return true; } 七 获取服务启动类型 /// <summary> /// 获取服务启动类型 2为自动 3为手动 4 为禁用 /// </summary> /// <param name="serviceName"></param> /// <returns></returns> public static string GetServiceStartType(string serviceName) { try { RegistryKey regist = Registry.LocalMachine; RegistryKey sysReg = regist.OpenSubKey("SYSTEM"); RegistryKey currentControlSet = sysReg.OpenSubKey("CurrentControlSet"); RegistryKey services = currentControlSet.OpenSubKey("Services"); RegistryKey servicesName = services.OpenSubKey(serviceName, true); return servicesName.GetValue("Start").ToString(); } catch (Exception ex) { return string.Empty; } } 八 验证服务是否启动 服务是否存在 /// <summary> /// 验证服务是否启动 /// </summary> /// <returns></returns> public static bool ServiceIsRunning(string serviceName) { ServiceController service = new ServiceController(serviceName); if (service.Status == ServiceControllerStatus.Running) { return true; } else { return false; } } /// <summary> /// 服务是否存在 /// </summary> /// <param name="serviceName"></param> /// <returns></returns> public static bool ServiceExist(string serviceName) { try { string m_ServiceName = serviceName; ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController s in services) { if (s.ServiceName.ToLower() == m_ServiceName.ToLower()) { return true; } } return false; } catch (Exception ex) { return false; } } 注:手动安装window服务的方法: 在“Visual Studio 2005 命令提示”窗口中,运行: 安装服务:installutil servicepath 卸除服务:installutil /u servicepath |
View Details
Thread thread=null; //定义线程 //开始线程 private void button1_Click(object sender, EventArgs e) { thread = new Thread(new ThreadStart(StartThread)); thread.Start();//开始线程 } private void StartThread() { int i = 0; while (true) { this.label1.Text = i.ToString(); //此处报错 i++; } } //结束线程 private void button4_Click(object sender, EventArgs e) { thread.Abort(); } 问题:当我们运行的时候会出现错误:"线程间操作无效: 从不是创建控件“label1”的线程访问它。" 解决方案:在程序运行的时候增加一句话,如下: public Form1() {InitializeComponent();Control.CheckForIllegalCrossThreadCalls = false; //这是增加的一句话 } 缺点:将UI传给了子线程,违背了弱耦合、封装的思想。子线程去更新UI的状态,如果有多个不同主线程要获取子线程状态,怎么办?
View Details
1 |
//十进制转二进制Console.WriteLine("十进制166的二进制表示: "+Convert.ToString(166, 2));//十进制转八进制Console.WriteLine("十进制166的八进制表示: "+Convert.ToString(166, 8));//十进制转十六进制Console.WriteLine("十进制166的十六进制表示: "+Convert.ToString(166, 16)); //二进制转十进制Console.WriteLine("二进制 111101 的十进制表示: "+Convert.ToInt32("111101", 2));//八进制转十进制Console.WriteLine("八进制 44 的十进制表示: "+Convert.ToInt32("44", 8));//十六进制转十进制Console.WriteLine("十六进制 CC的十进制表示: "+Convert.ToInt32("CC", 16)); |
View Details
定义
1 |
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">goto</span> 语句将程序控制直接传递给标记语句。 |
1 |
优点:具有了超强的灵活性<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />弱点:降低代码的可读性。<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />结构化编程所关注的就是解决单个功能函数内部的功能实现,所强调的是每个函数有“一个入口和一个出口”,而goto语句的使用却似乎违背了这样的准则。<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />但是,如果我们把goto语句限制在一个函数体内部使用,那么,goto语句还是会带来很多便利的。或许正是由于这一点,C#的缔造者也依然保留了功能强大的goto语句。 |
备注
1 |
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">goto</span> 的一个通常用法是将控制传递给特定的 <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">switch</span>-<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">case</span> 标签或 <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">switch</span> 语句中的默认标签。<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">goto</span> 语句还用于跳出深嵌套循环。 |
可以考虑使用goto的情形
1 |
1.从多重循环中直接跳出 <br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />2. 出错时清除资源 <br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />3.可增加程序的清晰度的情况。 |
不加限制地使用goto带来的弊端
1 |
1.破坏了清晰的程序结构,使程序的可读性变差,甚至成为不可维护的"面条代码"<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />2.经常带来错误或隐患。它可能跳过了某些对象的构造、变量的初始化、重要的计算等语句 |
示例 goto 在 switch 语句中的使用。
1 |
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); ">//</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "> statements_goto_switch.cs</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /></span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">using</span> System;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">class</span> SwitchTest<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />{<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">static</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">void</span> Main()<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> {<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Console.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">Coffee sizes: 1=Small 2=Medium 3=Large</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Console.Write(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">Please enter your selection: </span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">string</span> s = Console.ReadLine();<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span> n = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span>.Parse(s);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span> cost = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">0</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">switch</span> (n)<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> {<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">case</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">1</span>:<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> cost += <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">25</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">break</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">case</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">2</span>:<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> cost += <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">25</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">goto</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">case</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">1</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">case</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">3</span>:<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> cost += <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">50</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">goto</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">case</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">1</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">default</span>:<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Console.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">Invalid selection.</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">break</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> }<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">if</span> (cost != <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">0</span>)<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> {<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Console.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">Please insert {0} cents.</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>, cost);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> }<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Console.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">Thank you for your business.</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> }<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />} |
示例输出
1 |
Coffee sizes: 1=Small 2=Medium 3=Large<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />Please enter your selection: 2<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />Please insert 50 cents.<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />Thank you for your business. |
使用 goto 跳出嵌套循环
1 |
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); ">//</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "> statements_goto.cs<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /></span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); ">//</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "> Nested search loops</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /></span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">using</span> System;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">public</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">class</span> GotoTest1<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />{<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">static</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">void</span> Main()<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> {<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span> x = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">200</span>, y = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">4</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span> count = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">0</span>;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">string</span>[,] array = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">new</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">string</span>[x, y];<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); ">//</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "> Initialize the array:</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /></span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">for</span> (<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span> i = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">0</span>; i < x; i++)<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">for</span> (<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span> j = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">0</span>; j < y; j++)<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> array[i, j] = (++count).ToString();<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); ">//</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "> Read input:</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /></span> Console.Write(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">Enter the number to search for: </span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); ">//</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "> Input a string:</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /></span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">string</span> myNumber = Console.ReadLine();<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); ">//</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "> Search:</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 128, 0); "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /></span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">for</span> (<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span> i = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">0</span>; i < x; i++)<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> {<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">for</span> (<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">int</span> j = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 128); ">0</span>; j < y; j++)<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> {<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">if</span> (array[i, j].Equals(myNumber))<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> {<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">goto</span> Found;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> }<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> }<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> }<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Console.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">The number {0} was not found.</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>, myNumber);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(0, 0, 255); ">goto</span> Finish;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Found:<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Console.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">The number {0} is found.</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>, myNumber);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Finish:<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> Console.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">End of search.</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; color: rgb(128, 0, 0); ">"</span>);<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " /> }<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />}<br type="_moz" /> |
1 |
FROM URL:<a href="http://www.cnblogs.com/IT-Bear/archive/2012/03/05/2380389.html" style="background-color: rgb(255, 255, 255); ">http://www.cnblogs.com/IT-Bear/archive/2012/03/05/2380389.html</a> |
基于C#的Escape和Unescape实现 //在C#后台实现JavaScript的函数escape()的字符串转换//些方法支持汉字private string escape(string s){StringBuilder sb = new StringBuilder();byte[] byteArr = System.Text.Encoding.Unicode.GetBytes(s); for (int i = 0; i < byteArr.Length; i += 2){ sb.Append("%u");sb.Append(byteArr[i + 1].ToString("X2"));//把字节转换为十六进制的字符串表现形式 sb.Append(byteArr[i].ToString("X2"));}return sb.ToString(); }//把JavaScript的escape()转换过去的字符串解释回来//些方法支持汉字private string unescape(string s){ string str = s.Remove(0, 2);//删除最前面两个"%u"string[] strArr = str.Split(new string[]{"%u"}, StringSplitOptions.None);//以子字符串"%u"分隔byte[] byteArr = new byte[strArr.Length * 2];for (int i = 0,j=0; i < strArr.Length;i++,j+=2){byteArr[j + 1] = Convert.ToByte(strArr[i].Substring(0,2), 16); //把十六进制形式的字串符串转换为二进制字节byteArr[j ] = Convert.ToByte(strArr[i].Substring(2, 2), 16);}str = System.Text.Encoding.Unicode.GetString(byteArr); //把字节转为unicode编码return str; } 下面的为不支持汉字的转换: public static string Escape(string str){if (str == null)returnString.Empty; StringBuilder sb = newStringBuilder();int len = str.Length; for […]
View Detailsvar member = new Member(); int recordCount; var memberInfos = member.Paging(20, 1, "MemberId DESC", "", out recordCount); var memberIdSum = memberInfos.Sum(c => c.MemberId); Response.Write(memberIdSum.ToString()); Response.Write("<br/>——————————————————-<br />"); var dbHelper = DbFactory.AccessSqlServer; var dbPaging = DBPaging.Create(dbHelper); var ds = […]
View Details相对而言,LINQ TO DataSet是LINQ技术中最小的一块,虽然是DB中抽取出来的一个离线的操作模型,但毕竟对象也是个内存里面的object而已。所以和LINQ TO Object相比,大多数的操作都是一样的,不同只是要根据DataSet,DataTable的结构标明字段而已。下面简单的列出LINQ TO DataSet相比LINQ TO Object一些要注意的特色。 Query UnTyped DataSet 和一般的LINQ相比,query对象是untyped DataSet的时候,使用Field<T>和SetField<T>来读写不同的column字段,下面是一个简单的例子: DataTable orders = ds.Tables["Orders"]; DataTable orderDetails = ds.Tables["OrderDetails"]; var query = from o in orders.AsEnumerable() where o.Field<DateTime>( "OrderDate" ).Year >= 1998 orderby o.Field<DateTime>( "OrderDate" ) descending select o; 在这里大致要注意三点 1.因为untyped DataSet没有实现IEnumerable<T> 和 IQueryable<T>的interface,所以如果想把它作为一个可以查询的对象的话,要先用AsEnumerable() 或者AsQueryable()转换一下,将它转换成IEnumerable<T>或者IQueryable<T>对象才能用LINQ去查询。如:from o in orders.AsEnumerable() 2.一般是使用使用Field<T>(“Column A”)和SetField<T>(“Column A”)来读写不同的column字段对应的element,用它来访问相对于以前我们用ds.Tables["Orders"].Row[“RowA”][ “Column A”]的访问模式比起来,一个很大的好处就是可以避免null类型产生的exception。我们以前从DataSet里面取数据的时候,如果取的出来的是null,就会抛出exception,所以我们经常作类似if(ds.Tables["Orders"].Row[“RowA”][ “Column A”]!=null)的判断来包装我们进一步的逻辑处理,但是用Field<T>(“Column A”)就可以避免这种麻烦。因为Field<T>(“Column A”)是nullable的。这个特性的由来是<T>这个泛型的使用,比如你取int类型数据的时候,如果你觉得它可能是null,那你就可以用Field<int?>(“Column A”)去取,这样就可以避免了exception的抛出。 3 .Field<T>和SetField<T>是使用并不局限在LINQ 的query当中,在程序的其他地方也能使用,可以用它去替代以前的我们访问DataSet的方式,例如:
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2">foreach( DataRow r in orderDetails.Rows ) {</font></span> |
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> if (r.Field<decimal>( "UnitPrice" ) < 10 ){</font></span> |
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> r.SetField<decimal>( "UnitPrice", 10 );</font></span> |
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> }</font></span> |
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2">}</font></span> |
Query Typed DataSet 这就更加简单了。对于定义了类型的DataSet,我们可以象查询内存中一般的object那样去查询它。例如:
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2">var query =</font></span> |
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> from o in ds.Orders</font></span> |
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> where o.OrderDate.Year >= 1998</font></span> |
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> orderby o.OrderDate descending</font></span> |
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> select new { o.OrderID, o.OrderDate,</font></span> |
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> Amount = o.GetOrder_DetailsRows().Sum(</font></span> |
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> od => od.UnitPrice * od.Quantity ) };</font></span> |
还有一个与untyped DataSet不同的地方是在查询它的时候不需要使用AsEnumerable() 或者AsQueryable()那样的转换方法了。因为所有定义好的DataSet都是继承了TypedTableBase<T>这个基类,而这个基类已经实现了IEnumerable<T>的interface Query DataSet中的relation DataSet当中有时候也是有relation的,和DB一样,例如在下面的DataSet中加入relation:
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2">DataTable orders = ds.Tables["Orders"];</font></span> |
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2">DataTable orderDetails = ds.Tables["OrderDetails"];</font></span> |
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2">ds.Relations.Add( "OrderDetails",</font></span> |
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> orders.Columns["OrderID"],</font></span> |
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> orderDetails.Columns["OrderID"]);</font></span> |
如果我们想像在LINQ […]
View Details1.数据集( DataSet ) • DataSet 是更为广泛使用的ADO.NET 组件之一,它可以显式缓存不同数据源中的数据。 • 在表示层上DataSet 与GUI 控件紧密集成,以进行数据绑定。 • 在中间层上,它提供保留数据关系形状的缓存并包括快速简单查询和层次结构导航服务,从而可以减少对数据库的请求数。 2.查询数据集 • DataSet 虽然具有突出的优点,但其查询功能也存在限制。 • Select 方法可用于筛选和排序,GetChildRows和GetParentRow 方法可用于层次结构导航。 • 但对于更复杂的情况,开发人员必须编写自定义查询。这会使应用程序性能低下并且难以维护。 3.使用LINQ to DataSet • 使用LINQ to DataSet 可以更快更容易地查询在DataSet 对象中缓存的数据。 • 这些查询用编程语言本身表示,而不表示为嵌入在应用程序代码中的字符串。 • LINQ to DataSet 可使Visual Studio 开发人员的工作效率更高, 因为Visual Studio IDE 提供编译时语法检查、静态类型化和对 LINQ 的智能感知的支持。 • LINQ to DataSet 也可用于查询从一个或多个数据源合并的数据 这可以使许多需要灵活表示和处理数据的方案能够实现。 4.查询数据集 • 填充DataSet – XxxDataAdapter – LINQ to SQL •主要使用下面两个扩展类 – DataRowExtensions – DataTableExtensions • 查询支持 – 类型化数据集 – 非类型化数据集 5.启用LINQ to DataSet 功能 • 要求.NET Framework 3.5 • 引用System Data DataSetExtensions 程序集 […]
View DetailsLinQ家族五大成员:LinQ to Objects – 默认功能,用来实现对内存中集合对象的查询LinQ to SQL – 针对SQL Server的查询,它是一个带有可视化的操作界面的ORM工具LinQ to DataSet – 对强类型化或弱类型化的DataSet或独立的DataTable进行查询LinQ to Entity – 对实体框架中EDM定义的实体集合进行查询。LinQ to XML – 对XML文档进行查询创建等操作。 C#语法与LinQ相关的新增功能 1.隐式强类型变量在C#3.0中可以使用var关键字隐式定义强类型局部变量。 《图1》这里的var关键字定义变量与JavaScript定义变量看起来很像但二者有着本质的区别。JavaScript定义的变量是弱类型的变量,也可理解为是一种通用类型的变量,它可以容纳各种类型的值,还可以在运行过程中动态修改其中的内容类型。下面这种写法在JavaScript中是正确的:var obj = 3.14;obj = "hello world";C#中的var则是种强类型的变量,它在定义的时候会确定数据类型,分配内存空间。上面这两名代码在C#3.0中会报错,因为第一句已经把obj定义为double型的变量,第二句把字符串赋值给double是错误的操作。 隐式强类型并不能有效简化我的书写的代码,但当我们在用它来动态接收一些未知类型的数据的时候就显虽得很强大。在隐式强类型变量出现前,我们一般是使用object型变量来接收这种未知类型的数据的。 2.对象初始化这个功能可以有效简化类的getter和setter部份的代码,还可以只使用一行表达式语句实现对象的实例化与初始化操作。如定义实例类时可以使用如下代码,没有必要再把成员变量和属性分别定义了。public class LineItem{ public int OrderID { get; set; } public int ProductID { get; set; } public short Quantity { get; set; } public string QuantityPerUnit { get; set; } public decimal UnitPrice { get; set; } public float Discount { get; set; }} 实例化LineItem对象,并为它赋值var line3 = new LineItem { OrderID = 11000, ProductID = 61, Quantity = […]
View Details