随笔分类 -  C#

摘要:大家都知道foreach的语法:foreach(var item in items){ Console.Writeln(item);}通过这样一个简单的语句,就能实现遍历集合items中的所有元素。那么,集合类(注意不是集合中的元素的类)是要满足什么条件才能用foreach遍历呢?就像买衣服需要去找到卖衣服的人一样,要使用foreach,集合类必须要实现GetEnumerator()方法。这里的GetEnumerator就相当于商城里的售货员,穿着统一的服装,任务就是买衣服(程序里的任务是实现遍历集合)。当然了,售货员可以是个体户,也就是说,你可以自己直接实现GetEnumerator()方法 阅读全文
posted @ 2013-09-25 23:41 唐小喵 阅读(2376) 评论(2) 推荐(0) 编辑
摘要:索引器namespace 索引器{ class Program { static void Main(string[] args) { Person p1 = new Person(); p1[1] = "小明"; Console.WriteLine(p1[1] + p1[2]); Console.ReadKey(); } } public class Person { private string firstName = "大毛"; private string secondName = "二毛"; //下面这个就是索引器 publ 阅读全文
posted @ 2011-08-24 18:34 唐小喵 阅读(249) 评论(0) 推荐(0) 编辑
摘要:异常using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 异常{ class Program { static void Main(string[] args) { try { int i = Convert.ToInt32("abc"); } catch (Exception e) { Console.WriteLine("发生错误:" + e.Message + "\n异常堆栈:" + e.StackT 阅读全文
posted @ 2011-08-24 18:33 唐小喵 阅读(189) 评论(0) 推荐(0) 编辑
摘要:属性开头大写,字段开头小写namespace 属性{ class Program { static void Main(string[] args) { person p = new person(); p.Age = 30; Console.WriteLine(p.Age); person1 p1 = new person1(); Console.WriteLine(p1.Age);//自动赋初值0 Console.ReadKey(); } } class person { private int age; public int Age //这就是属性。拥有set和get Age并没有保存数 阅读全文
posted @ 2011-08-24 18:32 唐小喵 阅读(183) 评论(0) 推荐(0) 编辑
摘要:namespace refout参数{ class Program { static void Main(string[] args) { int age = 20; int age1 = 20; IncAge(ref age);//函数调用也要加ref IncAge1(out age1);//这里用out时,参数不需要初始化,函数里会把参数当做没初始化来用。把参数扔进去就行了,返回处理后的值。 Console.WriteLine(age); Console.ReadKey(); } static void IncAge(ref int age) //传址(引用)要加ref { age++; 阅读全文
posted @ 2011-08-24 18:30 唐小喵 阅读(203) 评论(0) 推荐(0) 编辑
摘要:字符串字符串可以当做字符数组处理,但是只能读,不能用数组的形式修改其中一位。如果非要修改,则必须用s.ToCharArray()方法得到字符串的字符数组。namespace 字符串中字符修改{ class Program { static void Main(string[] args) { string s1 = "hello"; char[] s2 = s1.ToCharArray();//将字符串转成字符数组以进行字符的修改 s2[2] = 'y'; string s3 = new string(s2);//调用构造函数new string (char 阅读全文
posted @ 2011-08-24 18:29 唐小喵 阅读(372) 评论(0) 推荐(0) 编辑
摘要:函数 都以static开头namespace 可变参数的函数{ class Program { static void Main(string[] args) { string[] names = { "Coco", "Lily", "Lucy" }; string country = "China"; VFunc(names); VFunc(country); SayHello("Tom", "张大虎", "狗剩"); Console.ReadKey() 阅读全文
posted @ 2011-08-24 18:27 唐小喵 阅读(201) 评论(0) 推荐(0) 编辑
摘要:@:表示后面的“\”不表示转义字符。转义符只针对在代码中直接写出的字符串。不针对从控制台读入的字符串。控制台读入什么就打印什么,不会转义。简单的类型转换Console.WriteLine("请输入一个数字:"); String s1 = Console.ReadLine(); //不能用int来接收 int i1 = Convert.ToInt32(s1); Console.WriteLine(i1);手机号或者密码中的数字只是看起来像数字。其实是字符串。Cast 类型转换不能在int和string之间进行强制类型转换。只能用convert转换。Enum 枚举的好处在于限定 阅读全文
posted @ 2011-08-24 18:26 唐小喵 阅读(415) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示