C#中"错误: 成员名称不能与它们的封闭类型相同"

本文介绍了一个关于 C# 中类的方法名与类名冲突的常见错误,并提供了修改建议。通过将方法名从 sum 改为 mysum 来解决编译时出现的错误。
部署运行你感兴趣的模型镜像

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

namespace aaa
{
public class Program
{
static int n, m,s;
static void Main(string[] args)
{

s=sum.sum(n,m);
Console.WriteLine(s);
}

}
public class sum
{
public static int sum( int a, int b)
{
return (a + b);
}
}
}
运行时提示:
错误“sum”: 成员名称不能与它们的封闭类型相同
public class sum
{
public static int sum( int a, int b)
{
return (a + b);
}
}


除了构造函数和析构函数之外,其它的函数不能跟类名相同。

把你的 public static int sum( int a, int b)
{
return (a + b);
}
改个名字,比如 mysum

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

错误 26 “StockWarningForm”: 成员名称不能它们的封闭类型相同 D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 58 26 千依零食 错误 3 类、结构或接口成员声明中的标记“{”无效 D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 57 1 千依零食 错误 2 类、结构或接口成员声明中的标记“namespace”无效 D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 56 1 千依零食 错误 24 无效的表达式项“.” D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 106 39 千依零食 错误 7 应输入 ) D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 46 27 千依零食 错误 9 应输入 ) D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 47 33 千依零食 错误 11 应输入 ) D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 48 28 千依零食 错误 13 应输入 ) D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 49 18 千依零食 错误 15 应输入 ) D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 50 21 千依零食 错误 17 应输入 ) D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 51 18 千依零食 错误 19 应输入 ) D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 52 18 千依零食 错误 21 应输入 ) D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 53 24 千依零食 错误 23 应输入 ) D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 54 27 千依零食 错误 1 应输入 } D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 54 28 千依零食 错误 4 应输入 class、delegate、enum、interface 或 struct D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 166 17 千依零食 错误 5 应输入类型、命名空间定义或文件尾 D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 178 5 千依零食 错误 6 语法错误,应输入“(” D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 46 21 千依零食 错误 8 语法错误,应输入“(” D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 47 7 千依零食 错误 10 语法错误,应输入“(” D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 48 7 千依零食 错误 12 语法错误,应输入“(” D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 49 7 千依零食 错误 14 语法错误,应输入“(” D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 50 7 千依零食 错误 16 语法错误,应输入“(” D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 51 7 千依零食 错误 18 语法错误,应输入“(” D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 52 7 千依零食 错误 20 语法错误,应输入“(” D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 53 7 千依零食 错误 22 语法错误,应输入“(” D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 54 7 千依零食 错误 25 语法错误,应输入“:” D:\Desktop\学习\C#\C#课设\千依零食\千依零食\StockWarningForm.cs 106 40 千依零食 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Data.OleDb; using System.Windows.Forms; namespace 千依零食 { public partial class StockWarningForm : Form { public StockWarningForm() { InitializeComponent(); } public class GoodsItem { public int ID { get; set; } public string GoodsName { get; set; } public int Stock { get; set; } public int WarnStock { get; set; } public string Unit { get; set; } // 使用 string.Format 替代 $"" 来格式化显示文本 public override string ToString() { return string.Format("{0} ({1}/{2}{3})", GoodsName, Stock, WarnStock, Unit); } } private void StockWarningForm_Load(object sender, EventArgs e) { // 初始化默认预警值为 5 numericUpDown1.Value = 5; // 加载一次数据 LoadWarningList((int)numericUpDown1.Value); } private void LoadWarningList() { using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Data.OleDb; using System.Windows.Forms; namespace 千依零食 { public partial class StockWarningForm : Form { public StockWarningForm() { InitializeComponent(); } public class GoodsItem { public int ID { get; set; } public string GoodsName { get; set; } public int Stock { get; set; } public int WarnStock { get; set; } public string Unit { get; set; } // 使用 string.Format 替代 $"" 来格式化显示文本 public override string ToString() { return string.Format("{0} ({1}/{2}{3})", GoodsName, Stock, WarnStock, Unit); } } private void StockWarningForm_Load(object sender, EventArgs e) { // 初始化默认预警值为 5 numericUpDown1.Value = 5; // 加载一次数据 LoadWarningList((int)numericUpDown1.Value); } private void LoadWarningList() { List<GoodsItem> warningItems = new List<GoodsItem>(); try { DataTable goodsTable = clsDB.ds.Tables["tGoods"]; if (goodsTable == null) { MessageBox.Show("商品数据未加载!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } foreach (DataRow row in goodsTable.Rows) { int stock = Convert.ToInt32(row["Stock"]); string goodsName = row["GoodsName"].ToString(); string unit = row["Unit"]?.ToString() ?? "个"; // 只要库存 <= 用户设置的预警值,就加入列表 if (stock <= warnValue) { GoodsItem item = new GoodsItem { ID = Convert.ToInt32(row["ID"]), GoodsName = goodsName, Stock = stock, WarnStock = warnValue, // 显示用,非真实字段 Unit = unit }; warningItems.Add(item); } } // 按库存升序排序 warningItems.Sort((x, y) => x.Stock.CompareTo(y.Stock)); // 更新 UI listBox1.DataSource = null; listBox1.Items.Clear(); if (warningItems.Count == 0) { listBox1.Items.Add("✅ 所有商品库存充足!"); label1.Text = "库存健康"; label1.ForeColor = Color.Green; } else { listBox1.DataSource = warningItems; label1.Text = string.Format("⚠️ 共 {0} 种商品需要补货!", warningItems.Count); label1.ForeColor = Color.Red; } } catch (Exception ex) { MessageBox.Show("加载预警数据失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void button1_Click(object sender, EventArgs e) { int userDefinedThreshold = (int)numericUpDown1.Value; LoadWarningList(userDefinedThreshold); } } } } private void button1_Click(object sender, EventArgs e) { int userDefinedThreshold = (int)numericUpDown1.Value; LoadWarningList(userDefinedThreshold); } } }
最新发布
12-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值