C#方法(函数)

2022-06-08  本文已影响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


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

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可变参数

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);
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读