2. c#从不同cs的文件调用函数

1.文件目录如下:

  1. Program.cs文件的主函数如下

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace datasAnalysis
    {
    internal static class Program
    {
    ///


    /// 应用程序的主入口点
    ///

    [STAThread]
    static void Main()
    {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
    }
    }
    }

3.Form1.cs文件的代码如下:

复制代码
using datasAnalysis.analysis;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace datasAnalysis
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ans ss = new ans();
            ss.outputs();

            txt_r hh = new txt_r();
            hh.outputs();

            for (int i = 0; i < 11; i++)
            {
                Debug.WriteLine(ss.width);
                Debug.WriteLine(hh.high);
            } 
        }
    }
}

以上文件调用了ans文件的outputs函数和txt_1文件的outputs函数。

4.ans.cs文件的代码如下:

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace datasAnalysis
{
    public partial class ans
    {
        public double width = 10;
        public double outputs()
        {
            return width;
        }

    }
}

5.txt_r.cs文件的代码如下:

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace datasAnalysis.analysis
{
    public class txt_r
    {

        public double high = 100;
        public double outputs()
        {
            return high;
        }

    }

}

6.代码运行结果如下:

以上就是不同cs文件的函数调用,经过测试运行结果正确无误。

相关推荐
啦啦啦_99992 分钟前
Redis-5-doFormatAsync()方法
数据库·redis·c#
春日见12 分钟前
车辆动力学:前后轮车轴
java·开发语言·驱动开发·docker·计算机外设
锐意无限16 分钟前
Swift 扩展归纳--- UIView
开发语言·ios·swift
低代码布道师16 分钟前
Next.js 16 全栈实战(一):从零打造“教培管家”系统——环境与脚手架搭建
开发语言·javascript·ecmascript
念何架构之路34 分钟前
Go进阶之panic
开发语言·后端·golang
亓才孓38 分钟前
[Properties]写配置文件前,必须初始化Properties(引用变量没执行有效对象,调用方法会报空指针错误)
开发语言·python
傻乐u兔41 分钟前
C语言进阶————指针3
c语言·开发语言
Porco.w1 小时前
C#与三菱PLC FX5U通信
网络·c#
两点王爷1 小时前
Java基础面试题——【Java语言特性】
java·开发语言
Swift社区1 小时前
Gunicorn 与 Uvicorn 部署 Python 后端详解
开发语言·python·gunicorn