C# object-oriented summary

1, ref and out parameters 

the only difference is that a variable you pass as an out parameter doesn't need to be initialised

using System;
namespace Ref_Out
{ 
    class test
	{
	   static void SomeFunction(out int i) 
      {
          i = 100; 
      }
	  
	   static void AnyFunction(ref int ii) 
      {
         ii = 200; 
      }
             
public static int Main() 
{
   int i; // note how i is declared but not initialized.
   int ii = 0;
   SomeFunction(out i);
   AnyFunction(ref ii);
   Console.WriteLine(i);  // will output 100
   Console.WriteLine(ii); // will output 200
   return 0; 
}
	}
}

2, C# support the optional arguments, allow some argument have default values 

void TestMethod(int optionalNumber = 10, int notOptionalNumber) 
{
    System.Console.Write(optionalNumber + notOptionalNumber); 
}

3, Method overloading

class ResultDisplayer 
{
   void DisplayResult(string result)
   {
      // implementation
   }
             
   void DisplayResult(int result)
   {
      // implementation
   } 
}

4, get & set Property for class / object

using System;
namespace test{

class test4{
   public static int Main()
   {
    string _n1, _n2;
	
    names n = new names();
	n.Name = "zhangxiao";
	_n1 = n.Name;
	Console.WriteLine(_n1);
	
	names2 n2 = new names2();
	n2.name = "kiddzhang";
	_n2 = n2.name;
	Console.WriteLine(_n2);
	
	return 0;
   }
}

class names{
    private string name;
	public string Name{
	   get
	   {
	     return name;
	   }
	   set
	   {
	     name = value;
	   }
	}		
}

class names2{
    public string name { get; set;}
}

}
Output:  zhangxiao

               kiddzhang


 constructors & static member (do not new)

using System;
namespace test{

class test4{
   public static int Main()
   {
    string _n3;
		
	names3 n3 = new names3("kidd");
	_n3 = n3.getname;
	Console.WriteLine(_n3);
	
	names4.op();
	
	return 0;
   }
}

class names3{
     private string names;
     public names3(string s)
	 {
	    this.names = s;
	 }
	
}

class names4{
    public static void op(){
	   Console.WriteLine("test output!");
	}
}

}


a static class cannot be instantiated. In other words, you cannot use the new keyword

Static methods can be overloaded but not overridden, because they belong to the class, and not to any instance of the class.







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值