为什么会发生在c#中 [英] why this happens in c#
问题描述
我不知道天气是否要求跟踪。
请明确怀疑。
public static列表与LT;产品> getproduct(){}
static public List< product> getproduct(){}
两者都被编译并调出时出错。
访问修改器的原因和关键字没有优先权?
或者我已经准备好在c#中基本错过了。
frends请告诉。
i dont know weather ask or not following .
please clear doubt.
public static List<product> getproduct(){}
static public List<product> getproduct(){}
both were compiled and called with out error.
why for access modfiers and keyword no priority ?
or i was all ready missed basic in c#.
frends pls tell.
推荐答案
修饰符没有必需的顺序,例如,请参阅此堆栈溢出问题: C#函数修饰符的顺序 [ ^ ]。
There is no required order for modifiers, see, for instance, this stack overflow question: C# order of function modifiers[^].
你的语句都不应该编译:你使用的是Generic List关键字而没有在一个角括号内指定一个Type。
另外,即使你正确指定了一个通用列表返回类型,这两个例子都不能编译,因为没有返回路径 在体内返回预期类型的方法。
如果你这样写:
Neither of your statements should compile: you are using the Generic List keyword without specifying a Type inside angle-brackets.
Also, even if you had correctly specified a Generic List return Type, both examples would not compile because there is no "return path" inside the body of the method which returns the expected Type.
If you wrote this:
public static List<int> getproduct()
{
return new List<int> {1,2,3};
}
static public List<string> getproduct()
{
return new List<string> {"a", "b"};
}
您也会收到错误,因为您已经定义了两个具有相同名称和相同参数规范的方法(在这种情况下:没有参数)。 br />
除了罕见的情况,比如类构造函数重载,最好为方法使用唯一的描述性名称!
术语助记符用于描述字段,属性,方法,类名等,帮助您立即了解它代表的内容......或者它的用途是什么,或者它是如何使用的。
而且,是的:.NET允许你按照自己的意愿重新命令使用'静态修饰符,但是:你只能使用一个保护修饰符!但是......去图......这将编译:
You'd also get an error because you have defined two methods with the same name and identical parameter specifications (in this case: no parameters).
Except for rare cases, like Class constructor overloading, it's a good idea to use unique and descriptive names for methods !
The term "mnemonic" is used to describe a field, property, method, Class name, etc., that helps you immediately understand what it represents ... or what it is for, or how it's used.
And, yes: .NET allows you to re-order the use of the 'static modifier as you wish, but: you can use only one "protection" modifier ! But ... go figure ... this will compile:
internal static protected void doNothing()
{
}
我强烈建议您单独研究辅助功能修饰符:公共,私有,内部,受保护等。[ ^ ]
I strongly suggest you study the accessibility modifiers individually: public, private, internal, protected, etc. [^]
这篇关于为什么会发生在c#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!