天天看點

外部方法、分部類分部方法

1、外部方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;    //添加外部類庫

namespace 外部方法
{
    class Program
    {
        [DllImport("User32.dll")]        //引用外部檔案
        public static extern int MessageBox(int h, string m, string c, int type);
        static int Main(string[] args)
        {
            Console.Title = "外部方法";
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Clear();

            string userName;
            Console.Write("請輸入使用者名:");
            userName = Console.ReadLine();
            Console.WriteLine();
            MessageBox(0, userName + "使用者,您好!", "消息框", 0);
            return 0;
        }
    }
}
           

2、分部類分部方法

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

namespace 分部類分部方法
{
    public partial class Student
    {
        private string studentID;
        private string studentName;
        public Student(string id,string name)
        {
            studentID = id;
            studentName = name;
            this.oncreate();
        }
        public void ShowMsg()
        {
            Console.WriteLine("學号:{0};姓名:{1}\n", studentID, studentName);
        }
        partial void oncreate();
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "分部類分部方法!!";
            Console.BackgroundColor = ConsoleColor.Yellow;
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Clear();

            string id;
            string name;
            Console.Write("輸入學号:");
            id = Console.ReadLine();
            Console.Write("輸入姓名:");
            name = Console.ReadLine();
            Student Myobject = new Student(id,name);
            Myobject.ShowMsg();
            Console.Read();
        }
    }
}
           
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 分部類分部方法
{
    public partial class Student
    {
        partial void oncreate()
        {
            Console.WriteLine("Student類執行個體建立成功!\n");
        }
    }
}