Package is defined as "the one or more items enclosed in a physical or logical packets." In object-oriented design methodology, the package is to prevent access to implementation details.
C # package according to specific needs, a user is provided access to, and through the access modifiers to achieve
C # supports access modifier as follows
public | All objects are accessible |
private | Objects can be accessed inside the object itself |
protected | Only the object class and subclass object can be accessed |
internal | Object same assembly can access |
protected internal | Access is limited to this type of assembly, or derived from the class comprising |
If no access modifier, use the default access modifier class members, namely Private .
One method is to put some relevant statements grouped together, to perform a task block. Each program has a C # class with at least the Main method.
1. 方法定义
<Access Specifier> <Return Type> <Method Name>(Parameter List)
{
Method Body
}
2. Method Invocation
Examples of calls and call class (instance of the class name. Method name / class name. Method name), a recursive call
When you call a method with parameters, you need to pass parameters to the method. In C #, there are three ways to pass parameters to the method:
Value transfer | The default transfer mode. In this manner the actual value to the parameter copy function formal parameters, arguments and parameters used are two different values in memory. In this case, when the parameter value changes, does not affect the value of the argument, the actual parameters to ensure data security. |
Passed by reference | In this way the memory location to copy the parameters of a reference to the formal parameters. This means that, when the parameter value is changed, but also to change the value of the argument |
Output transfer | This way you can return multiple values. |
In C #, using the ref keyword to declare a reference parameter, using the out keyword to declare the output transfer
Variables provided to the output parameters of the assignment is not required. When the need to return a value from the method parameter is not specified in the initial value of the output parameter is particularly useful
/* 参数传递*/
using System;
public class Argstest
{
class SwapTest
{
// 值传递
public void swapV(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
//引用传递
public void swpaQ(ref int a, ref int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
//输出传递
public void getValue(out int x, out int y)
{
int temp = Convert.ToInt32(Console.ReadLine());
x = temp;
y = x*x;
}
}
class TestMain
{
static void Main(string[] args)
{
SwapTest sw = new SwapTest();
int a = 345;
int b = 567;
sw.swapV(a,b);
Console.WriteLine("a: {0}",a);
Console.WriteLine("b: {0}",b);
sw.swpaQ(ref a,ref b);
Console.WriteLine("a: {0}",a);
Console.WriteLine("b: {0}",b);
sw.getValue(out a, out b);
Console.WriteLine("a: {0}",a);
Console.WriteLine("b: {0}",b);
Console.ReadLine();
}
}
}