C#入门例子

一、属性

在属性的访问声明中:
◆只有set访问器,表明属性的值只能进行设置而不能读出。
◆只有get访问器,表明属性的值是只读的,不能改写。
◆同时具有set访问器和get访问器,表明属性的值的读写都是允许的。
除了使用了abstract修饰符的抽象属性,每个访问器的执行体中只有分号“;”,其它所有属性的get访问器都通过return来读取属性的值,set访问器都通过value来设置属性的值。

using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  ConsoleApplication1
{
    
class Man
    
{
        
private int _x;
        
public Man(int xx)
        
{
            
this._x = xx;
        }

        
public int x
        
{
            
get
            
{
                Console.WriteLine(
"get x: {0}", _x);
                
return _x;
            }

            
set
            
{

                Console.WriteLine(
"set x:{0} a new value {1}", _x, value);
                _x 
= value;
            }

        }

      
        
public static Man operator ++(Man man)
        
{
            man.x 
= man.x + 100 ;//先运行一次 x.get,再运行一次 x.set
            return man;
        }

    }



    
class Program
    
{
        
static void Main(string[] args)
        
{
            Man man 
= new Man(33);
            Console.WriteLine(
"befor add:x-{0}", man.x);//运行一次 x.get
            man++;                                    
            Console.WriteLine(
"befor add:x-{0}", man.x);//运行一次 x.get
            return;
        }

    }

}

运行结果:

get x: 33
befor add:x-33
get x: 33
set x:33 a new value 133
get x: 133
befor add:x-133

每次 运行 man.x ,set  或者 get 都会自动运行,对属性进行读或者写。

感觉优点怪怪的,不过确实蛮方便的。

二、异常处理

异常处理貌似和java一样。

执行代码抛出异常,catch捕获异常,finally执行收尾工作(finally不管是否捕获异常都会执行)。

叁、装箱与拆箱
@装箱是将值类型转换为引用类型
@拆箱是将引用类型转换为值类型
@利用装箱和拆箱功能,可通过允许值类型的任何值与 Object 类型的值相互转换,将值类型与引用类型链接起来

装箱  值类型的数据转换为object类
      隐式转换
拆箱  将封装的对象转换为值
      显式转换
is运算符   <operand>  is  <type>
   <operand> 可转换为<type> true
as运算符   <operand>  as  <type>

int  val  =   100 ;
object  obj  =  val;     // 装箱:值类型到引用类型
int  num  =  ( int ) obj;  // 拆箱:引用类型到值类型
// 被装过箱的对象才能被拆箱???

四、C#通过提供索引器,可以象处理数组一样处理对象。特别是属性,每一个元素都以一个get或set方法暴露。

写了个实验的,好像长了点,晕。。。。个先。。。 

    class MyName                   //姓名对象
    {
        
private string _first_name; //
        private string _second_name;//
        public MyName(String first_name, String second_name)
        
{//构造函数
            this._first_name = first_name;
            
this._second_name = second_name;
        }

        
public String FirstName
        
{//属性
            get return _first_name; }
            
set this._first_name = value; }
        }

        
public String SecondName
        
{////属性
            get return _second_name; }
            
set this._second_name = value; }
        }

    }

    
class MynameList  //测试索引器
    {
        
private MyName[] names;

        
public MynameList(int capacity)
        
{
            
this.names = new MyName[capacity];
        }

        
public MyName this[String first_name]//用姓索引
        {
            
get
            
{
                
// 遍历数组中是否存在该姓的对象,若存在返回第一个找到的对象
                foreach (MyName curName in names)
                
{
                    
if (curName.FirstName == first_name)
                        
return curName;
                }

                Console.WriteLine(
"未找到");
                
return null// 使用 null 指示失败

            }

        }

        
public MyName this[int index] //用下标索引
        
{
            
get
            
{
                
// 验证索引范围
                if (index < 0 || index >= names.Length)
                
{
                    Console.WriteLine(
"索引无效");
                    
return null;// 使用 null 指示失败
                }

                
return names[index]; // 对于有效索引,返回请求的对象
            }

            
set
            
{
                
if (index < 0 || index >= names.Length)
                
{
                    Console.WriteLine(
"索引无效");
                    
return;
                }

                names[index] 
= value;
            }

        }

    }

五、委托

 
[ 属性 ] [  委托修饰符 ]  delegate  返回类型  标识符  ( [形参表] );
委托声明定义一种类型,它用一组特定的参数以及返回类型封装方法。对于静态方法,委托对象封装要调用的方法。对于实例方法,委托对象同时封装一个实例和该实例上的一个方法。如果您有一个委托对象和一组适当的参数,则可以用这些参数调用该委托。委托的一个有趣且有用的属性是,它不知道或不关心自己引用的对象的类。任何对象都可以;只是方法的参数类型和返回类型必须与委托的参数类型和返回类型相匹配。这使得委托完全适合“匿名”调用。

  delegate   void  MyDelegate( string  s); // 定义委托
     class  MyClass
    
{
        
public static void Hello(string s)
        
{
            Console.WriteLine(
"  Hello, {0}!", s);
        }

        
public static void Goodbye(string s)
        
{
            Console.WriteLine(
"  Goodbye, {0}!", s);
        }

 
        
static void Main(string[] args)
        
{
            MyDelegate a, b, c, d;
            
// 创建委托对象
            a = new MyDelegate(Hello);   //将方法hello与委托关联起来
            b = new MyDelegate(Goodbye); //将方法Goodbye与委托关联起来
            c = a + b; d = c - a; 
            Console.WriteLine(
"Invoking delegate a:"); 
            a(
"A");
            Console.WriteLine(
"Invoking delegate b:");
            b(
"B");
            Console.WriteLine(
"Invoking delegate c:");
            c(
"C");
            Console.WriteLine(
"Invoking delegate d:");
            d(
"D");
            
return;
        }

    }

一旦为委托分配了方法,委托将与该方法具有完全相同的行为。委托方法的使用可以像其他任何方法一样。

 

c# msdn:http://msdn2.microsoft.com/zh-cn/library/67ef8sbd(VS.80).aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值