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

相关推荐
名字还没想好☜3 小时前
Next.js ‘use client‘ 到底加在哪:Server/Client Components 边界与常见报错
开发语言·前端·javascript·react·next.js
初级代码游戏4 小时前
iOS开发 Swift 速记1:变量和基本数据类型
开发语言·ios·swift
海盗12344 小时前
微软技术周报 ——2026-08-03
后端·python·microsoft·c#·.netcore
酷在前行5 小时前
【R生态】PERMANOVA 进阶实战:距离选择、PERMDISP、两两比较与受限置换(保姆级教程)
开发语言·r语言
cxr8285 小时前
Graphify vs GitNexus vs CodeGraph — 三工具架构深度对比
开发语言·架构·知识图谱
废弃的小码农6 小时前
功能测试--Day07--Python编程基础
开发语言·python
fengci.6 小时前
Microweber CMS 未授权路径穿越漏洞(CVE-2026-65694)
android·开发语言·前端·学习·php
程序员zgh7 小时前
C++ 拷贝赋值运算符 详解
c语言·开发语言·c++
念何架构之路7 小时前
restartmanager-重启管理子系统
java·开发语言
SomeB1oody8 小时前
【RustyML入门】2.0. 经典机器学习
开发语言·后端·机器学习·rust·教程