c#学习笔记
2011-11-25 21:03
375 查看
*属性: 属性开头字母大写
属性可以判断输入的非法值
属性本身不存储值->依靠字段
*索引器
*有中文用nvarchar
*SQL语句中字符串用单引号
属性可以判断输入的非法值
属性本身不存储值->依靠字段
*索引器
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace b { class Program { static void Main(string[] args) { //Robot r1 = new Robot(); //Robot r2 = new Robot(); //r1.Name = "1"; //r1.Eat(5); //Console.WriteLine("hi"); //while(true) //{ // Console.WriteLine("please input the string"); // string str = Console.ReadLine(); // r1.Speak(str); //} //try //{ // int i = Convert.ToInt32("abc"); //} //catch (Exception e) //{ // Console.WriteLine(e.Message + "@" + e.StackTrace); //} //Console.ReadKey(); person p = new person(); p[1] = 2; p[2] = 3; Console.WriteLine(p[1]); } } class Robot { private string name; private int food; public string Name { get { return name; } set { this.name = value; } } private int FullLevel { get { return food; } set { this.food = value; //避免如此写法 //this.FullLevel = value; } } public void SayHello() { Console.WriteLine("My name is {0}",Name); } public void Eat(int foodCount) { if(foodCount > 100) { return; } FullLevel += foodCount; } public void Speak(string str) { if(FullLevel <= 0) { Console.WriteLine("stop"); return; } if(str.Contains("name") || str.Contains("Name")) { this.SayHello(); } else if (str.Contains("gril")) { Console.WriteLine("suck"); } else { Console.WriteLine("unkonwn"); } FullLevel--; } } class person { private int a; private int b; public int this[int index] { set { if (index == 1) { a = value; } else if(index ==2) { b = value; } else { throw new Exception ("error"); } } get { if (index == 1) { return this.a; } else if(index ==2) { return this.b; } else { throw new Exception("error1"); } } } } }
*有中文用nvarchar
*SQL语句中字符串用单引号
相关文章推荐
- C#学习笔记
- C#学习笔记(十):阴影和深度复制
- C#学习笔记(十三):读写Txt文件
- C#学习笔记系列之二:delegate
- C#学习笔记(2)--.NET 代码编译过程
- C#学习笔记(三)
- C#学习笔记(十一)
- c#学习笔记(一)
- c#学习笔记《1》——regex类(个人理解)
- C#学习笔记(五)(Winform窗体)(一)
- C#学习笔记(二)
- C#学习笔记(十)-实现Windows程序的数据更新
- C#学习笔记(6)委托
- C#学习笔记
- C#学习笔记
- C#学习笔记(三):值类型、引用类型及参数传递
- C#学习笔记——连接sqlserver数据库
- C#学习笔记------3、继承
- C#学习笔记(一)
- C#学习笔记(三)—–C#高级特性:try语句和异常