Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 6-Dec-13 3:08am
v2
Comments
Sergey Alexandrovich Kryukov 6-Dec-13 12:21pm    
Showing one line without context makes no sense at all.
Your question itself also makes no sense. You ask "why" after a false statement about priority. There is no such concept, and should not be. There are syntax rules you have to follow.
—SA
rajacsharp5 9-Dec-13 7:41am    
CPallini cleard doubt.
iam just asking why the follwing 2 syntaks accepting in c#.
public static List getproduct(){}
static public List getproducts(){}
for this entire code not required.

There is no required order for modifiers, see, for instance, this stack overflow question: C# order of function modifiers[^].
 
Share this answer
 
Comments
rajacsharp5 6-Dec-13 10:13am    
thanks
Sergey Alexandrovich Kryukov 6-Dec-13 12:23pm    
Sure, 5ed. Also, this page teaches to distinguish modifiers and other syntactic elements, such as return type...
—SA
CPallini 6-Dec-13 13:55pm    
Thank you, Sergey.
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:
C#
public static List<int> getproduct()
{
    return new List<int> {1,2,3};
}

static public List<string> getproduct()
{
    return new List<string> {"a", "b"};
}
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:
C#
internal static protected void doNothing()
{
}
I strongly suggest you study the accessibility modifiers individually: public, private, internal, protected, etc. [^]
 
Share this answer
 
v2
Comments
Hamassss 6-Dec-13 9:33am    
Basicly what i wanted to write, just forgot List types :) +5 from me!
rajacsharp5 6-Dec-13 10:12am    
Hi thans for your revert.
Sergey Alexandrovich Kryukov 6-Dec-13 12:27pm    
Strictly speaking, List could be acceptable. The OP's problem is not showing the context.

So, how it could work? I'll show it:


using List = System.Collections.Generic.List<string>;

//...
public static List GetProduct(){ /* ... */ }


Simple, isn't it?

However, it does not make OP's question quite correct.

—SA
BillWoodruff 7-Dec-13 2:14am    
I cannot think of a worse practice than obfuscating the behavior of .NET operators using ... 'using ! Hair will grow on your tongue if you do this :)
Sergey Alexandrovich Kryukov 7-Dec-13 18:10pm    
Wait a second: do you call "using A = namespace.A" obfuscation? First, I really think this is the strong exaggeration on your side. Of course, making a mess out of it would be bad, but I regularly use this method in certain select cases. Say, I indent to use only one list type (or so), and don't want to repeat the instantiation of the template. This is a bit different philosophy of program text support. I would rather say "it's a matter of personal taste". I can give you many scenarios of support of code where it is beneficial. In some cases, even use the pattern "using A = namespace.B". Can explain why.

After all, I hope you cannot argue against user types. By their nature is the same: you give one single name you prefer to some structure; this way, you reuse the definition of this structure and give it a shorter name. This is not obfuscation, really.

But, to get back to the point, I used this example only for a formal argument that the type List can appear in code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900