美文网首页
C#方法(函数)

C#方法(函数)

作者: 山猪打不过家猪 | 来源:发表于2022-06-08 16:01 被阅读0次

方法调用

namespace cSharpTest
{
    class fxx
    {
        static void Main(string[] args)
        {
            //调用,类名.方法
            fxx.Print();
            Console.WriteLine(fxx.AddNum(1, 2));
        }
        //public:访问修饰符:公开的
        //static:静态的
        //返回值类型:如果不需要写返回值,写void
        //方法名:首字母全部大写
        public static int AddNum(int n1,int n2)

        {
            return n1 > n2 ? n1 : n2;//返回一个int
        }

        public static void Print()
        {
            Console.WriteLine("This is a print func");
        }
    } 
}

静动态方法和实例方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;



namespace test2
{
    class Program
    {
        static void Main(string[] args)
        {

            //实例方法
            Person p1 = new Person();
            p1.sayHello();
            int age1= p1.sayAge();
            Console.WriteLine(age1);
            //静态方法调用,直接用类名.方法
            Person.sayId();
            string cn1 = Person.sayClass();
            Console.WriteLine(cn1);
            Console.ReadLine();


        }
        public class Person
        {

            //公开的public,void无返回的, sayhello()的函数
            public void sayHello()
            {
                Console.WriteLine("I am a person.");
            }
            //返回值是int类型的实例方法
            public int sayAge()
            {
                return 12;
            }
            //静态方法
            //返回值为空的静态方法
            public static void sayId()
            {
                Console.WriteLine("My id is 001;");

            }

            public static string sayClass()
            {
                return "Person";

            }
        }
    }
}

C#无全局变量


namespace cSharpTest
{
    class fxx
    {
        public static int global_a = 10;//使用静态字段,模拟全局变量

        static void Main(string[] args)
        {
            int num_compare = fxx.AddNum(global_a, 2);
            Console.WriteLine(num_compare);
        }

        public static int AddNum(int n1,int n2)

        {
            return n1 > n2 ? n1 : n2;

    } 
}

out

  • 用于多个不同的返回类型
  • 如果一个方法中,返回多个相同类型的值,可以只返回一个数组
  • 如果返回多个不同类型的值的时候,就不行了,可以考虑使用out参数
  • out 方法必须在方法的内部赋值

namespace cSharpTest
{
    class fxx
    {


        public struct LoginInfor
        {
            public string _usm;
            public string _pwd;
        }

        static void Main(string[] args)
        {

            LoginInfor Fxx;
            string usm_fxx =Fxx._usm = "admin";
            string pwd_fxx= Fxx._pwd = "123";
            string msg;
            bool is_login = fxx.IsLogin(usm_fxx, pwd_fxx, out msg);
            Console.WriteLine("Login result:{0}", is_login);
            Console.WriteLine("Login message:{0}", msg);
        }
        
        public static bool IsLogin(string usm, string pwd, out string login_mes)
        {
            if (usm =="admin" && pwd=="123")
            {
                login_mes = "login success";
                return true;
            }
            else
            {
                login_mes = "login failed";
                return false;
            }
        }

    } 
}

ref

  • 用于不写return同步改值
  • 能够将一个变量带入方法中改变,改变完成后,在把改变完成后的值,带出方法,不需要写return
namespace cSharpTest
{
    class fxx
    {
        static void Main(string[] args)
        {
            double ffx_salary = 4000;
            string show_fxx_salary = fxx.Jiangjia(ref ffx_salary);

            Console.WriteLine("salary of fxx:{0}", ffx_salary);
            Console.WriteLine(show_fxx_salary);
        }

        public static string Jiangjia(ref double salaries)
        {
            salaries -= 1000;
            string fxx_show = String.Format("After fxx was kicked,her salary was {0}", salaries);
            return fxx_show;
        }
    } 
}

params可变参数

  • 把参数类型一样的参数,当做方法params 定义的数组的元素;
  • params必须是形参最后一位且只有一个;
namespace cSharpTest
{
    class fxx
    {
        static void Main(string[] args)
        {
            
            //int[] ss = { 2, 1, 23, 4 };//声明一个数组
            AddNum("fxx",2,1,23,4);//把后面的数字,当成一个all_num数组
        }

        public static void AddNum(string name, params int[] all_num)
        {

            int sum = 0;
            for (int i = 0; i < all_num.Length; i++)
            {
                sum += all_num[i];

            }

            Console.WriteLine("{0}总数是:{1}",name, sum);

        }
    } 
}

方法重载

namespace _04class
{
    class _04fxx
    {
        static void Main(string[] args)
        {
            ReRusult("123", "41424");
            ReRusult(10, 100);
            
        }

        static void ReRusult(int _num01, int _num02)
        {
            int _num03 = _num01 + _num02;
            Console.WriteLine(_num03);
        }

        //函数重载
        static void ReRusult(string _str01,string _str02)
        {
            string _str3 = _str01 + _str02;
            Console.WriteLine(_str3);
        }
    }
}

相关文章

  • C#调用OpenCV函数的实现

    C#调用OpenCV函数的实现步骤:1、C++编写调用OpenCV函数的方法,编译成dll;2、C#引用C++生成...

  • Swift中函数的初级操作

    介绍 Swift 函数与 javascript 和 C# 的函数写法很像,但和 Objective-C 的方法却大...

  • C#方法(函数)

    方法调用 C#无全局变量 out 用于多个不同的返回类型 如果一个方法中,返回多个相同类型的值,可以只返回一个数组...

  • C# 委托

    C#委托 C#中的委托(Delegate)类似于C或C++中函数的指针。委托(Delegate)是存有对某个方法的...

  • Golang研学:在函数、方法、接口中用好指针类型

    Golang研学:在函数、方法、接口中用好指针类型 在大部分面向对象语言如C++、C#、Java,在函数传参数时除...

  • C# 委托(Delegate)

    C# 中的委托(Delegate)类似于 C 或 C++ 中的函数指针。委托(Delegate) 是存有对某个方法...

  • Unity使用C++读取xml文件时的一个问题记录

    Unity使用C++读取文件的方法:C++侧: C#侧: 这里在使用时如果将CSharpReadBytes函数得到...

  • 2021-02-18【Dart】语法记录

    1.命名构造函数 c#改写:可以使用静态函数:

  • C#类和结构

    C#类和结构 结构 特点 结构可带有方法、字段、索引、属性、运算符方法和事件 结构可以定义构造函数,但不能定义析构...

  • tensorflow的sess.run详解

    以下函数注释方法来源于c#语言。 ///这个是让fetches节点动起来,告诉tensorflow,想要此节点的输...

网友评论

      本文标题:C#方法(函数)

      本文链接:https://www.haomeiwen.com/subject/dtvumrtx.html