   General C# Quirks
  This is not flamebait, so I hope no one gets too excited.I have been well-versed in C++ and Java for several years now so I decided to start looking a little at C# to compare the languages and try to stay in the loop.I''ve only started going through the C# Tutorials but I had some questions/concerns, hopefully someone well-versed in C# can offer their opinions/advice/knowledge.  Basically it seems that C# is an attempt to fix things in C/C++ very similar to the way Java addressed these concerns.  My main beef is1) Why doesn''t the Main() method need to be public?2) What is the reason for having both ref and out type parameters modifiers?  It seems to me that ref would have been sufficient and I don''t see any added value to specifying an out parameter only.  I am sure the compiler does not issue an error if you assign a value to a variable before passing it as an out parameter, so I don''t get it.  Can anyone clarify for me?  3) Also, why not just use the "&" operator to indicate a reference, maybe they felt that the & is not explicit enough and also might confuse people thinking it has to do with C++''s "address-of" operator.4) Why add back in the "goto" statement after it was removed in "Java"?  "goto" is universally panned as a development practice, yet the C# spec does not even try to justify keeping this.  And if you say "To promote easy porting of C++", I feel things like ref/out break portability way more than "goto" does.  The "goto" mechanism is archaic as witnessed by some fo the rules:  "If a goto statement is inside a try that includes a finally block, and the labeled statement is outside the try, and the end point of the finally block is unreachable, then the labeled statement is not reachable from that goto statement."5) Why the "no-fall-through" rule for switch statements?  This only seems to promote the use of goto 6) Why not include bit fields?7) Some features also in Java seem implemented in C# in such a way as to simply distinguish themselves from Java.  I guess it''s because Microsoft doesn''t want to admit to borrowing concepts well-proven in Java due to the ownership of Java:  sealed vs. final, base vs. super.  Why not just give "props" to Java and use the same keyword???  Does someone think Sun would have sued them for the similarity?  It would also ease portability. As I said, I''ve just started learning C# so my ignorance is probably pretty apparent.Well, unfortunately, this email turned into more of a "gripe-about-changes-in-C# vs. Java/C++" type of thread than I wanted it to be, but hopefully it doesn''t spark a flame war.  Bear in mind that I do find a lot of good concepts in C# over Java from my reading thus far:- type system unification- support of rectangular and jagged arrays- foreach statement (only a shortcut to be sure, but still useful)- delegates (still in the learning stages)Thanks,Jeff 
quote:Original post by rypyr   1) Why doesn't the Main() method need to be public?Why would it? Do you need other classes to run your Main method? No, so why would it need to be public?quote:2) What is the reason for having both ref and out type parameters modifiers?  It seems to me that ref would have been sufficient and I don't see any added value to specifying an out parameter only.  I am sure the compiler does not issue an error if you assign a value to a variable before passing it as an out parameter, so I don't get it.  Can anyone clarify for me?  Declaring something 'ref' allows you to change the original by creating a reference. However, the original must have some value before it is passed as a 'ref' paramater. You cannot pass an uninitialized variable as 'ref.'Declaring something 'out' forces you to change the original by creating a reference. 'out' is basically like having another return value, the compiler will error if you do not assign a value to an 'out' parameter during the function.quote:5) Why the "no-fall-through" rule for switch statements?  This only seems to promote the use of goto Because well written code shouldn't need to do that.   [edited by - jperalta on January 29, 2004 12:04:38 PM];  OK Ima give some of these a shot, but not all quote:2) What is the reason for having both ref and out type parameters modifiers?  It seems to me that ref would have been sufficient and I don''t see any added value to specifying an out parameter only.  I am sure the compiler does not issue an error if you assign a value to a variable before passing it as an out parameter, so I don''t get it.  Can anyone clarify for me?  To further enforce parameter restrictions. The value of an out parameter will not be passed to the function and need not be initialized, but must be assigned a value before it returns. If you don''t assign something to an out parameter in the function, you get an error. Pretty nifty IMO quote:3) Also, why not just use the "&" operator to indicate a reference, maybe they felt that the & is not explicit enough and also might confuse people thinking it has to do with C++''s "address-of" operator.Actually, most parameters are passed by reference auto-magically in C# (save value types and probably some other rules I don''t know). The & operator in C++ indicated that it would be the case, but its absence guaranteed a pass-by-value. So they are completely different things.quote:4) Why add back in the "goto" statement after it was removed in "Java"?  "goto" is universally panned as a development practice, yet the C# spec does not even try to justify keeping this.  And if you say "To promote easy porting of C++", I feel things like ref/out break portability way more than "goto" does.  The "goto" mechanism is archaic as witnessed by some fo the rules:  "If a goto statement is inside a try that includes a finally block, and the labeled statement is outside the try, and the end point of the finally block is unreachable, then the labeled statement is not reachable from that goto statement."5) Why the "no-fall-through" rule for switch statements?  This only seems to promote the use of goto Answered your own question . To justify #5, a fall-though-case in C++ is rarely done on purpose the first time it is written. Sometimes, you get code that acts just a BIT funny because of a missing break statement, but you can''t figure out why. This is just one more method of having the compiler catch your errors before they surface as ugly bugs. quote:6) Why not include bit fields?A better question is: Why include bit fields? True, sometimes a human-bit-packed structure is better then what the compiler does, but not always. Sometimes bit-fields in C++ serve only to obfuscate and slow otherwise compiler-optomizable code ^_^;quote:7) Some features also in Java seem implemented in C# in such a way as to simply distinguish themselves from Java.  I guess it''s because Microsoft doesn''t want to admit to borrowing concepts well-proven in Java due to the ownership of Java:  sealed vs. final, base vs. super.  Why not just give "props" to Java and use the same keyword???  Does someone think Sun would have sued them for the similarity?  It would also ease portability. I think because C++ uses the term ''base class'' and not ''super class'' more often then not, so I think base makes more sense. Also, ''final'' might be confused with ''finally'', but not a big difference there, I admit.quote:As I said, I''ve just started learning C# so my ignorance is probably pretty apparent.Well, unfortunately, this email turned into more of a "gripe-about-changes-in-C# vs. Java/C++" type of thread than I wanted it to be, but hopefully it doesn''t spark a flame war.  Bear in mind that I do find a lot of good concepts in C# over Java from my reading thus far:- type system unification- support of rectangular and jagged arrays- foreach statement (only a shortcut to be sure, but still useful)- delegates (still in the learning stages)Thanks,Jeff   I wouldn''t call you ignorant at all  I wish I had the answer to # 1, though .... ;quote:1) Why doesn''t the Main() method need to be public?why does it matter? the main method is basically irrelevant to me except for setting up and launching my application.quote:2) What is the reason for having both ref and out type parameters modifiers?  It seems to me that ref would have been sufficient and I don''t see any added value to specifying an out parameter only.  I am sure the compiler does not issue an error if you assign a value to a variable before passing it as an out parameter, so I don''t get it.  Can anyone clarify for me?This is just one of those explicit use deals ... sure ref and out can be used in the exact same way, but if your code will not be using the referenced value of a parameter, only setting the outward-bound value, then out is what you''d want to use.quote:3) Also, why not just use the "&" operator to indicate a reference, maybe they felt that the & is not explicit enough and also might confuse people thinking it has to do with C++''s "address-of" operator.Well, you can still use the & when working with pointers in an unsafe context. article.quote:4) Why add back in the "goto" statement after it was removed in "Java"?  "goto" is universally panned as a development practice, yet the C# spec does not even try to justify keeping this.  And if you say "To promote easy porting of C++", I feel things like ref/out break portability way more than "goto" does.  The "goto" mechanism is archaic as witnessed by some fo the rules:  "If a goto statement is inside a try that includes a finally block, and the labeled statement is outside the try, and the end point of the finally block is unreachable, then the labeled statement is not reachable from that goto statement."I could go either way on this one ... just cuz it''s there doesn''t mean you have to use it.quote:5) Why the "no-fall-through" rule for switch statements?  This only seems to promote the use of goto  ... no comment on this onequote:6) Why not include bit fields?That is a good idea ...quote:7) Some features also in Java seem implemented in C# in such a way as to simply distinguish themselves from Java.  I guess it''s because Microsoft doesn''t want to admit to borrowing concepts well-proven in Java due to the ownership of Java:  sealed vs. final, base vs. super.  Why not just give "props" to Java and use the same keyword???  Does someone think Sun would have sued them for the similarity?  It would also ease portability. I don''t really see anything wrong with this one ... quote:- delegates (still in the learning stages)Delegates and Events Rock!   Joel Martinezhttp://www.codecube.net/ ;quote:Original post by joelmartinez  quote:- delegates (still in the learning stages)Delegates and Events Rock!   They will rock even more when anonymous methods come about in C# 2.0 ^_^;quote:Original post by Rick Scott  They will rock even more when anonymous methods come about in C# 2.0 ^_^Hell yeah!  and C#''s overall rockingness will ++ even more with generics ... imagine, iterating over a collection with no boxing/unboxing!  Joy! Joel Martinezhttp://www.codecube.net/ ;quote:Original post by joelmartinez  quote:Original post by Rick Scott   They will rock even more when anonymous methods come about in C# 2.0 ^_^Hell yeah!  and C#''s overall rockingness will ++ even more with generics ... imagine, iterating over a collection with no boxing/unboxing!  Joy! Joel Martinezhttp://www.codecube.net/   Yes, very drool-able I agree. Also, its a minor point, but having to explicitly cast your values back is a tad annoying, especially when you forget to do it...Error: Cannot convert type (object) to type (int) in ArrayList...hehe ;  Thanks for the clarification on differences between ref and out.  So the compiler will throw an error if my method doesn't assign a value to an out parameter for ALL possible return paths (I wonder: including throwing exceptions?)... Yes, that is kinda nifty.In terms of a public Main method, my feelings on "public" is that the method can be called or "visible" outside the class.  To me, "outside" includes the runtime environment.  But I can adjust my thinking...   [edited by - rypyr on January 29, 2004 12:20:11 PM];  Also C# features better than Java:- static constructors- enums (btw, can enums be declared inside classes?  declared public, etc?) ;quote:Original post by rypyr  Thanks for the clarification on differences between ref and out.  So the compiler will throw an error if my method doesn''t assign a value to an out parameter for ALL possible return paths (I wonder: including throwing exceptions?)... Yes, that is kinda nifty.Wouldn''t it be niftier to just be able to return multiple values? 