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文件的函数调用,经过测试运行结果正确无误。

相关推荐
唐青枫18 小时前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫2 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m6252 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户91721561902112 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠3 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫5 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech5 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf7 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6257 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
Artech7 天前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf