创建并引用库文件

本文详细介绍了如何使用C#创建一个计算平均数的库文件,并展示了两种在WindowsFormsApplication中引用该库文件的方法,包括手动复制dll文件和通过添加引用。代码示例中展示了如何实例化库类并调用方法计算数组的平均值。
摘要由CSDN通过智能技术生成

创建并引用库文件



一、用C#创建并编写库文件

打开 Visual Studio 2015并新建类库文件并命名为average,程序编写完成后点击运行,生成库类文件。

代码如下(数字之间以空格分开,求取平均数):

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

namespace average
{
    public class Class1
    {
        public double AVER(string inputString)
        {
            IList<string> input = inputString.Split(' ');
            IList<double> input2 = new List<double>();
            double temp = 0;
            for (int i = 0; i < input.Count; i++)
            {
                if (!double.TryParse(input[i], out temp))
                {                    
                   break;
                }
                else
                {
                    input2.Add(temp);
                }
            }
            Calc(input2);
            return Calc(input2);
        }


        private static double Calc(IList<double> input)
        {
            double temp = 0;
            foreach (double var in input)
            {
                temp += var;
            }
            double average = temp / input.Count;
            return average;
        }
    }
}

二、库文件的引用

1.创建添加控件WindowsFormsApplication文件并添加控件

        private System.Windows.Forms.RichTextBox richTextBox1;
        private System.Windows.Forms.Button aver;
        private System.Windows.Forms.RichTextBox richTextBox2;
        private System.Windows.Forms.Button array;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.NumericUpDown MAX;
        private System.Windows.Forms.NumericUpDown MIN;
        private System.Windows.Forms.Label label3;

2.库文件的引用方法一

打开库文件的average→average→bin→Debug复制average.dll文件,并将库文件放在新建WindowsFormsApplication文件的 求平均数→求平均数→bin→Debug中,此时我们可以通过输入代码对average库进行引用。

代码如下(示例):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;//引用库文件

namespace 求平均数
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        /// <summary>
        /// 创建数组
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void array_Click(object sender, EventArgs e)
        {
            try
            {
                richTextBox1.Clear();
                Random rd = new Random();
                int stu = Convert.ToInt32(textBox1.Text);//创建数组的数量
                int max = Convert.ToInt32(MAX.Value);//最大值
                int min = Convert.ToInt32(MIN.Value);//最小值
                for (int i = 0; i < stu; i++)
                {
                    richTextBox1.Text += rd.Next(min, max).ToString() + " ";//创建数组
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());//显示问题
            }
        }

        /// <summary>
        /// 取平均数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void aver_Click(object sender, EventArgs e)
        {
            average.Class1 averAge = new average.Class1();//实例化
            double ad = averAge.AVER(richTextBox1.Text);
            richTextBox2.Text = ad.ToString();
        }
    }
}

3.库文件的引用方法一

新建WindowsFormsApplication,右击引用→添加引用→浏览→average.dll,添加

using average;

代码如下(示例):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using average;

namespace 求平均数
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 创建数组
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void array_Click(object sender, EventArgs e)
        {
            try
            {
                richTextBox1.Clear();
                Random rd = new Random();
                int stu = Convert.ToInt32(textBox1.Text);//创建数组的数量
                int max = Convert.ToInt32(MAX.Value);//最大值
                int min = Convert.ToInt32(MIN.Value);//最小值
                for (int i = 0; i < stu; i++)
                {
                    richTextBox1.Text += rd.Next(min, max).ToString() + " ";//创建数组
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());//显示问题
            }
        }

        /// <summary>
        /// 取平均数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void aver_Click(object sender, EventArgs e)
        {
            Class1 averAge = new Class1();
            double ad = averAge.AVER(richTextBox1.Text);
            richTextBox2.Text = ad.ToString();
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值