2017-03-02

out  关键字指定所给的参数为一个输出参数 该参数的值将返回给函数调用中使用的变量

注意事项 1未赋值的变量用作ref参数是非法的,但是可以把未赋值的变量用作out参数

2 在函数使用out参数时,必须把它看成尚未赋值

例如:

static void Main(string[] args)
{
//int myNunber = 5;
//Console.WriteLine("mynumber is {0}",myNunber);
//DoubleValue(ref myNunber);
//Console.WriteLine(myNunber);

int[] myArray = { 1,4,2,5,6,7,8,0,9,3};
int maxIndex;
Console.WriteLine(MaxValue(myArray,out maxIndex));
Console.WriteLine($"{maxIndex+1}");

Console.ReadKey();
}

public static int MaxValue(int[] myArray, out int maxIndex)
{
int maxValue = myArray[0];
maxIndex = 0;
for (int i = 0; i < myArray.Length; i++)
{
if (myArray[i] > maxValue)
{
maxValue = myArray[i];
maxIndex = i;
}
}
return maxValue;
}

ref 引用传参 函数处理的变量和函数调用中使用的变量相同,而不仅仅是值相同的变量。对这个变量做的任何的改变都会影响用作参数的变量值 用ref关键字指定参数

注意 两个限制 1 必须在函数调用中使用"非常量的变量"  2必须使用初始化过的变量   const 定义该变量始终为常量 限制一个变量被改变,只读变量 在定义的时候初始化,之后不能被更改

例如:

namespace Ref和Out
{
class Program
{
static void Main(string[] args)
{
int myNunber = 5;
Console.WriteLine("mynumber is {0}",myNunber); //5
DoubleValue(ref myNunber);//10
Console.WriteLine(myNunber);//10
Console.ReadKey();

}
public static void DoubleValue(ref int val)
{
val =val* 2;
Console.WriteLine($"the value is {val}");
}

}
}

Params

当需要传递多个参数或者参数的个数不确定的时候,就可以使用params类型的参数。例如

namespace Params
{
class Program
{
static void Main(string[] args)
{
UserParams(1,2,3);
UserParamsTwo("1","test",true);
int[] myArray = { 1, 2, 3, 4, 5 };
UserParams(myArray);
Console.ReadKey();

}

public static void UserParams(params int[] list)
{
for (int i = 0; i < list.Length; i++)
{
Console.WriteLine(list[i]);
}
Console.WriteLine();
}
public static void UserParamsTwo(params object[] list)
{
foreach (var i in list)
{
Console.WriteLine(i);
}
Console.WriteLine();
}
}

}

1 构造函数

作用是初始化对象,对对象的属性依次的赋值 没有返回值 没有void 构造函数的名称必须和类名一致 构造函数也可以实现重载

创建对象的时候调用构造函数 每个类都会有一个默认的无参数的构造函数 当显示的声明了一个构造函数后,默认的无参数的构造函数就被干掉了

2 重载

函数名称相同,参数的个数或者类型不同

3 this

1代表当前类的对象 2 显示的调用当前类的构造函数

4 构造函数调用构造函数

public Student(string name,int age,char gender,int chinese,int math,int english)
{
this.Name = name;
this.Age = age;
this.Gender = gender;
this.Chinese = chinese;
this.Math = math;
this.English = english;
}

:this

public Student(string name, int age, char gender):this(name,age,gender,0,0,0)
{
//this.Name = name;
//this.Age = age;
//this.Gender = gender;
}

5 属性 property  字段 fileds  方法 method

作用是保护字段 对字段的取值和赋值做限制 其本质就是一个get方法 一个set方法

6 析构函数

程序执行结束的时候 执行析构函数 现在c#中有一个GC垃圾回收机制

7 继承  基类/父类 派生类/子类

子类隐式的获得父类的除构造函数和析构函数以为的所有成员 ???

子类只能有一个直接父类 所以c#不能实现多重继承  而父类可以有多个直接子类 所以继承是可以传递的;

如果class b 派生出 class c ; class a 派生出 class b 那么 class c会继承 class a 和class b的声明的成员;

可以实现某个类禁止被其他类继承吗?  c#提供了一个 sealed修饰符  会阻止其他类继承改基类

//父类 person

public class person

{

//字段

private string _name;

private int _age;

private char _gender;

//属性

public string Name {get;set;}

public int Age {get;set;}

pubic char Gender {get;set;}

//构造函数

public person(string name,int age,char gender)

{

  this.Name=name;this.Age=age;this.Gender=gender;

}

//自定义函数

public void SayHello()

{

  console.writeline("我是{0},我今年{1}岁了,我是{2}生",this.Name,this.Age,this.Gender);

}

}

//第一个子类 reporter 记者类  继承父类 person

public class repoter:person

{

//字段

private string _hobby;

//属性

public string Hobby{get;set;}

//构造函数 继承父类的

public reporter(string name,int age,char gender,string hobby):base(name,age,gender)

{

  this.Hobby=hobby;

}

//自定义函数

public void reporterSayHello()

{

  console.writeline("我是{0},我今年{1}岁了,我是{2}生,我的爱好是{3}",this.Name,this.Age,this.Gender,this.Hobby);

}

}

//第二个子类 programmer 继承父类person

public class programmer:person

{

//字段

private int _iWorkYear;

//属性

public int iWrokYear{get;set;}

//构造函数 继承父类

pulic programmer(string name,int age,char gender,int iWorkYear):base(name,age,gender)

{

  this.iWrokYear=iWorkYear;

}

//自定义函数

public void programmerSayHello()

{

  console.writeline("我是{0},我今年{1}岁了,我是{2}生,我的工作年限是{3}",this.Name,this.Age,this.Gneder,this.iWorkYear);

}

}

最新文章

  1. [Android]使用Dagger 2依赖注入 - 图表创建的性能(翻译)
  2. .NET LINQ 相等运算
  3. Candies-POJ3159差分约束
  4. 为什么占位符可以防止sql注入?
  5. css三角形
  6. ember.js:使用笔记10 常用方法
  7. js 标签云效果
  8. Javascript兼容和CSS兼容总结
  9. 微信分享朋友圈监听(PHP)
  10. SQL Server 的三种用户自定义函数
  11. Android SystemUI源代码分析和修改
  12. BCP导入导出MsSql
  13. IIS8中使用OpenSSL来创建CA并且签发SSL证书
  14. jquery1.8.3和1.11.3的用法区别
  15. [Poi2010]Monotonicity 2 线段树
  16. python_递归_斐波那契
  17. 关于原生js中bind函数的实现
  18. RabbitMQ 使用QOS(服务质量)+Ack机制解决内存崩溃的情况
  19. Django03-视图系统views
  20. 使用RStudio调试(debug)基础学习(一)

热门文章

  1. redis的key对应mysql数据表设计
  2. Java基础——类和对象的初始化过程
  3. js 手机号码简单正则校验
  4. android UI卡顿问题学习
  5. QQ自动登录里的一些控件知识
  6. JAVA实现两种方法反转单列表
  7. iOS开发——GPUImage源码解析
  8. [置顶] 使用 maven 插件 maven-shade-plugin 对可执行 java 工程及其全部依赖 jar 进行打包
  9. [luogu2513 HAOI2009] 逆序对数列 (计数dp)
  10. VS2015 C# 编写USB通信上位机时,改变net框架导致DLL调用失败的问题解决方法