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.