C#函数重载

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//函数重载
/*
 * 重载定义:同一个类中的方法同名,参数不同,调用时根据实参的形式选择与之匹配的方法执行操作的一种技术
 * 参数不同表现为:
 * 1.参数类型不同;
 * 2.参数的个数不同;
 * 3.参数的顺序不同;
 * 适用范围:
 * 普通方法,构造方法
 * 
*/
namespace ConsoleApplication3
{
    class Program
    {
        #region
        //static void Main(string[] args)
        //{
        //}
        //public void Statr()
        //{ }
        参数个数不同
        //private void Num()
        //{ }
        //private void Num(int a)
        //{ }
        参数类型不同
        //public void Type(int a)
        //{ }
        //public void Type(string a)
        //{ }
        参数顺序不同,两个以上参数只要有一个顺序不同就能重载
        //public void Sore(int a, string b)
        //{ }
        //public void Sore(string a, int b)
        //{ }
        返回类型不同,不能重载
        //public int Return(int a)
        //{
        //    return 0;
        //}
        //public string Return()
        //{
        //    return null;
        //}
        参数名不同也不能重载
        //public void Study(int score)
        //{ }
        //public void Study(int scoreNum)
        //{ }
        下面两个是函数重载
        //private static void Temp(int num)
        //{
        //}
        //private static void Temp(int[] nums)
        //{
        //}
        #endregion
        #region//根据数值可以调用不同的函数
        //static void Main(string[] args)
        //{
        //    //根据数值可以调用不同的函数
        //    Add(2,5);
        //    Add(1.2f,2.3f);
        //    Add("a","b");
        //    Add(1,1.1f);
        //    Console.ReadKey();
        //}
        //public static void Add(int a, int b)
        //{
        //    Console.WriteLine(a + b);
        //}
        //public static void Add(float a, float b)
        //{
        //    Console.WriteLine(a + b);
        //}
        //public static void Add(string a, string b)
        //{
        //    Console.WriteLine(a + b);
        //}
        //public static void Add(int a, float b)
        //{
        //    Console.WriteLine(a + b);
        //}
        #endregion
        #region//传值
        //private static int[] num = new int[5] { 0, 1, 2, 3, 4 };
        //static void Main(string[] args)
        //{
        //    //传值
        //    Console.WriteLine(num[0]);
        //    Temp(num);
        //    Console.WriteLine(num[0]);
        //    Console.ReadKey();
        //}
        //private static void Temp(int num)
        //{


        //}
        //private static void Temp(int[] nums)
        //{
        //    nums[0] = 100;
        //}
        #endregion
        static void Main(string[] args)
        { 
            //三种转换方式
            int a = Convert.ToInt16(1.5f);//四舍五入
            int b = int.Parse("1");
            int c = (int)1.5f;//最常见
            Console.WriteLine(a);
            Console.ReadKey();
        }
        public static int Parse(string numString)
        {
            //两种转换方式
            //int numInt = Convert.ToInt32(numString);
            int numInt = int.Parse(numString);
            return Convert.ToInt32(numString);
        }
        public static int Parse(float numFloat)
        {
            //两种转换方式
            //int numInt = (int)numFloat;
            int numInt = Convert.ToInt32(numFloat);
            return (int)numFloat;
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值