C#学习——外部方法与分部方法

一.外部方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Runtime.InteropServices;//Alt+Shift+F10快速引用
    namespace _8._6_外部方法
    {
        //方法位置:通常类当中,并且与其他方法保持平级关系 
        class Program
        {
            //使用之前应该引入命名空间:System.Runtime.InteropServices
            [DllImport("User32.dll")]
            //声明外部方法 使用关键字extern由于配合DLLImport属性使用,所以必须包含static关键字
            public static extern int MessageBox(int h,string m,string c,int type);
            static int Main(string[] args)
            {
                Console.Write("请输入您的姓名:");
                string name = Console.ReadLine();
                //利用return进行弹出对话框,所以需要将Main方法改为有返回值
                return MessageBox(0,"您好:"+name+"\n\n"+"欢迎来到二胖的微博","欢迎提示",0);
            }
        }
    }


结果显示

二.分部方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _8._7_分部方法
{
    public partial class Program
    {
        //分部方法必须在分部类或分部结构中进行声明和定义
        //声明分部方法
        //方法默认为私有,也可以人为加上private
        partial void Write();//声明
        partial void Write()//分部方法定义
        {
            Console.WriteLine("这是一个分部方法");
        }
    }
   public partial class Program
    {
        static void Main(string[] args)
        {

            //调用分部方法
            Program p = new Program();
            p.Write();
            Console.ReadKey();
        }
    }
}

结果显示
注:
(1)分部方法只能为Void,默认为Private修饰
(2)分部方法不能有Virtual和Extern方法
(3)分部方法可以有ref参数,不能有out参数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值