C#语法基础(笔记)

1.类的基本概念

1.类的声明

  • 类的声明并不创建类的实例,但是创建了用于创建实例的模板。
  • 和C++相比,类没有全局函数(全局方法),所有的方法都必须声明在类的里面,同样也没有默认的返回类型,所有的方法都必须要包含返回类型和 void
  • 要为实际数据分配内存,就要用new运算符。

声明类的语句

StudentList studentList = new StudentList();

声明类的实例的时候,后边要加() ,是为了方便传递构造函数的


2.访问修饰符

  1. private(私有的):只能从类的内部访问。
  2. protected(受保护的)
  3. internal(内部的)
  4. protected internal(受保护的内部的)

private 是默认的访问类型,但是即使是某个字段被声明为 private, 依然还可以在类的任何地方被访问。代码如下:

 class Test{
      private  int a=3;private int b=5;
      
       private int Add3()return a;
       
       private int Add2()  return b;
      
       public int Add(){
          return Add2()+Add3();
       }
    };
    class Program {
        static void Main (string[] args) {
            Test test=new Test();
            System.Console.WriteLine(test.Add());
        }
    }

3.方法

1.void 方法

void 方法不需要返回语句,当控制流走到大括号关闭的地方,控制就返回到调用代码,并且没有值被插入到调用代码之中。

在符合特定条件的情况下,我们提前退出方法,不用带参数:

  • 使用return
  • 只能用于 void 声明的方法

2.引用参数(ref)

参数要求:

  • 引用参数,必须在声明调用 使用 ref 修饰符
  • 实参必须是变量,因为他要地方存它,而且在作为实参前必须要被赋值,如果是引用类型变量,可以赋值为一个 引用或者 null

使用 ref 这个不需要用 return 语句

  class Myclass {
        public int Val = 20;
    };
    class Program {

        static void MyMethod (ref Myclass f1, ref int f2) {
            f1.Val = f1.Val + 5;
            f2 = f2 + 5;
            System.Console.WriteLine ($"Mycalss is {f1.Val}   f2 is {f2}");
        }
        static void Main (string[] args) {
           Myclass a1=new Myclass();
           int a2=10;
           MyMethod(ref a1,ref a2);
           System.Console.WriteLine($"f1.val: {a1.Val},  f2:{a2}");
        }
    }

输出结果:

在这里插入图片描述



3.输出参数(out)

参数要求和引用(ref)参数一样

  • 必须在声明和调用中都使用修饰符
  • 实参必须是变量。这是有道理的,因为方法需要内存位置保存返回值

用于从方法体内,把数据传出到调用代码。

和引用参数不同的是:

  • 参数的初始值是无关的。而且没有必要在方法调用之前为实参赋值。
  • 在方法内部,输出参数是在被读取之前被赋值的
  • 在方法返回之前,方法内部贯穿的任何可能路径都必须为所有输出参数进行一次赋值。

代码如下:

class Myclass {
        public int Val = 10;
    };
    
class Program {
            static void MyMethod (out Myclass f1, out int f2) {
// 写代码的时候,不用管他传进来的是什么,都当作是输出来看待,
//只要输出有了就行。
            f1 = new Myclass ();
            f1.Val = 20;
            f2 = 25;
        }
        static void Main (string[] args) {
            Myclass a = new Myclass ();
            a.Val = 50;
            int b = 30;
            Program.MyMethod (out a, out b);
            System.Console.WriteLine (a.Val);
            Syste

输出

20
25

out 的总结:

  • 在输入函数前的初始化没有什么用。进了函数里面,都得从头初始化。
  • 写里面的内容的时候,就把穿进来的参数 ,当输出写就行。

4.方法的重载

  1. 一个类中,可以有一个以上的方法,拥有相同的名称,这叫做 方法的重载。但是相同的每个方法,必须有一个和其他地方不相同的 签名

  2. 方法签名包括:

    • 方法的名称
    • 参数的数目
    • 参数的类型和数目
    • 参数的修饰符

    返回类型不是参数的一部分,而我们往往认为它是签名的一部分。

在这里插入图片描述


5.命名参数和可选参数

一般当参数比较多的时候,采用这两个方法:

1.命名参数

class MyProgam {
        public int Calc (int a, int b, int c) {
            return (a + b) * c;
        }
    }
    class Program {
        static void Main (string[] args) {
            MyProgam myProgam = new MyProgam ();
            
            int result = myProgam.Calc (a: 3, c: 9, b: 1);
            
            System.Console.WriteLine (result);
        }
    }

2.可选参数:

注意,可选参数,一定要写在所有的必选参数的后边。

  class MyProgam {
        public int Calc (int a, int c,int b=5) {
            return (a + b) * c;
        }
    }
    class Program {
        static void Main (string[] args) {
            MyProgam myProgam = new MyProgam ();
            
            int result = myProgam.Calc (a:3, c:9);
            
            System.Console.WriteLine (result);
        }

6.静态字段

静态字段被类的所有实例所共享,所有实例都是访问同一内存位置。如果该位置上的值被一个实例改变了,这种改变对所有的实例都可见。


    class MyProgam {
        public static int a;
        public int m;
    }

通过实例 “ .” 不出来,只能通过类名来引用:

在这里插入图片描述

posted @ 2020-09-04 00:08  沧海一声笑rush  阅读(43)  评论(0编辑  收藏  举报