C#编程:定义类成员-定义字段
在类定义中也提供了类成员的定义.此篇说下定义类字段.定义类字段时要用到关键字.
分别为:public, private, internal, protected, static, readonly
Public : 成员可以由任何代码访问
ptivate : 成员只能由类中的代码访问.如果没有使用任何关键字默认就是使用这个
internal : 成员只能由定义它的项目内部的代码访问
protected : 成员只能能由类或派生类中的代码访问
static : 声明类的静态成员
readonly : 声明类的成员只能在执行构造函数的过程中赋值
代码:
- class Myclass
- {
- public int MyInt = 1;
- private int MyInt2 = 2;
- internal int MyInt3 = 3;
- protected int MyInt4 = 4;
- public readonly int MyInt5 = 5;
- public static int MyInt6 = 6;
- }
Read the rest of this entry »
Add new tag, C#编程, internal, private, protected, public, readonly, static, 字段, 类成员
C#编程:使用params关键词定义返回值函数
定义带返回值的函数通常有四种样式,此篇说前两种.定义一个函数的代码样式static int MaxValue(int[] numArry)或static int MaxValue(params int[] numArry).这串的区别在于params关键词.
params关键词:使用它可以接受任意个参数或不接受任何参数.
下面是练习时写的两个函数分别用上面说的两种形式来定义函数.来看下它们的区别;
- static int MaxValue(int[] numArry)
- {
- int MaxValue = numArry[0];
- for (int i = 1; i < numArry.Length; i++)
- {
- if (MaxValue < numArry[i])
- MaxValue = numArry[i];
- }
- return MaxValue;
- }
-
- static int MinValue(params int[] numArry)
- {
- int MinValue = numArry[0];
- for (int i = 1; i < numArry.Length; i++)
- {
- if (MinValue > numArry[i])
- MinValue = numArry[i];
- }
- return MinValue;
- }
Read the rest of this entry »
C#编程, params, static, 关键词, 返回值函数
C#编程:定义和使用函数static void Write()
C#编程定义函数有两种1:无返回值的函数(static void 函数名())void指没有返回值.2有返回值的函数(static int/string/double….. 函数名())).
看代码示例:
1:无返回值的函数
- static void Print()
- {
- Console.WriteLine("函数内容!");
- }
-
- static void Main(string[] args)
- {
- Print();
- //使用方法
- }
1:有返回值的函数.遍历数组中的最大值
- static int maxNum(int[] intArr)
- {
- int maxNum = intArr[0];
- for (int i = 1; i < intArr.Length; i++)
- {
- if (intArr[i] > maxNum)
- maxNum = intArr[i];
- }
- return maxNum;
- }
-
- static void Main(string[] args)
- {
- Print();
- //Console.ReadKey();
- int[] intArr = { 9, 11, 2, 2, 1, 4 };
- Console.WriteLine("数组中最大值为:{0}", maxNum(intArr));
- Console.ReadKey();
- }
C#编程, static, static void, 使用函数, 定义函数