C#编程:定义类成员-定义字段
在类定义中也提供了类成员的定义.此篇说下定义类字段.定义类字段时要用到关键字.
分别为:public, private, internal, protected, static, readonly
Public : 成员可以由任何代码访问
ptivate : 成员只能由类中的代码访问.如果没有使用任何关键字默认就是使用这个
internal : 成员只能由定义它的项目内部的代码访问
protected : 成员只能能由类或派生类中的代码访问
static : 声明类的静态成员
readonly : 声明类的成员只能在执行构造函数的过程中赋值
代码:

  1. class Myclass
  2.         {
  3.             public int MyInt = 1;
  4.             private int MyInt2 = 2;
  5.             internal int MyInt3 = 3;
  6.             protected int MyInt4 = 4;
  7.             public readonly int MyInt5 = 5;
  8.             public static int MyInt6 = 6;
  9.         }

Read the rest of this entry »

, , , , , , , , ,

C#编程:使用params关键词定义返回值函数
定义带返回值的函数通常有四种样式,此篇说前两种.定义一个函数的代码样式static int MaxValue(int[] numArry)或static int MaxValue(params int[] numArry).这串的区别在于params关键词.
params关键词:使用它可以接受任意个参数或不接受任何参数.
下面是练习时写的两个函数分别用上面说的两种形式来定义函数.来看下它们的区别;

  1. static int MaxValue(int[] numArry)
  2.         {
  3.             int MaxValue = numArry[0];
  4.             for (int i = 1; i < numArry.Length; i++)
  5.             {
  6.                 if (MaxValue < numArry[i])
  7.                     MaxValue = numArry[i];
  8.             }
  9.             return MaxValue;
  10.         }
  11.  
  12.         static int MinValue(params int[] numArry)
  13.         {
  14.             int MinValue = numArry[0];
  15.             for (int i = 1; i < numArry.Length; i++)
  16.             {
  17.                 if (MinValue > numArry[i])
  18.                     MinValue = numArry[i];
  19.             }
  20.             return MinValue;
  21.         }

Read the rest of this entry »

, , , ,

C#编程:定义和使用函数static void Write()
C#编程定义函数有两种1:无返回值的函数(static void 函数名())void指没有返回值.2有返回值的函数(static int/string/double….. 函数名())).
看代码示例:
1:无返回值的函数

  1. static void Print()
  2.         {
  3.             Console.WriteLine("函数内容!");
  4.         }
  5.  
  6.         static void Main(string[] args)
  7.         {
  8.             Print();
  9.  //使用方法
  10.         }

1:有返回值的函数.遍历数组中的最大值

  1. static int maxNum(int[] intArr)
  2.         {
  3.             int maxNum = intArr[0];
  4.             for (int i = 1; i < intArr.Length; i++)
  5.             {
  6.                 if (intArr[i] > maxNum)
  7.                 maxNum = intArr[i];
  8.             }
  9.             return maxNum;
  10.         }
  11.  
  12.         static void Main(string[] args)
  13.         {
  14.             Print();
  15.             //Console.ReadKey();
  16.             int[] intArr = { 9, 11, 2, 2, 1, 4 };
  17.             Console.WriteLine("数组中最大值为:{0}", maxNum(intArr));
  18.             Console.ReadKey();
  19.         }

, , , ,