Celebrity Deathmatch (VB.NET vs C#)
-
Does
Func<bool>
accept non-bool funcs? If not the other C# examples here only accept bool too. Or am I missing the point here?See the tip/trick I just posted. You don't have to use
Func<bool>
. You can use whatever return type you need. You can even useFunc<Object>
if you like. I'm not sure, but I think covariance or contravariance (I forget which is which) may allow you to still avoid having to wrap the functions in a new lambda. -
AspDotNetDev wrote:
Maybe "End Sub" makes the code easier to read to somebody not initiated with the language, but it doesn't make the code any easier to write
Which is a reasonable trade-off. You write code once and read it many times. Besides, with any decent editor, it is a non-issue.
AspDotNetDev wrote:
if (true)
{I see unnecessary and confusing symbols here. For instance in Go, it would be something like:
if true {
Or (even better) in ML:
if true then
#define then {
-
Yes, it can, but you want the process to complete each routine until it reaches a true condition, so your code has a bug in it (correct version): static void Main(string[] args) { bool s = !step1() && !step2() && !step3(); Console.Read(); } static bool step1() { Console.WriteLine("Step 1"); return false; } static bool step2() { Console.WriteLine("Step 2"); return true; } static bool step3() { Console.WriteLine("Step 3"); return false; }
-
Yes, it can, but you want the process to complete each routine until it reaches a true condition, so your code has a bug in it (correct version): static void Main(string[] args) { bool s = !step1() && !step2() && !step3(); Console.Read(); } static bool step1() { Console.WriteLine("Step 1"); return false; } static bool step2() { Console.WriteLine("Step 2"); return true; } static bool step3() { Console.WriteLine("Step 3"); return false; }
Nope, 'cechode' wrote: General Re: Celebrity Deathmatch (VB.NET vs C#) Pin member cechode 23hrs 30mins ago i'm not a vb guy anymore but when i was i LOVED exit on first failed step. ( instead of nested if's ) BTW, I would use
bool s = step1() || step2() || step3();
to execute until first true result. -
VB.NET supports by-ref extension methods. As of 4.0, C# does not.
Regards, Nish
Are you addicted to CP? If so, check this out: The Code Project Forum Analyzer : Find out how much of a life you don't have! My technology blog: voidnish.wordpress.com
Nishant Sivakumar wrote:
VB.NET supports by-ref extension methods. As of 4.0, C# does not.
WHAT!!! I don't have 4.0 version. This continues to work in 3.5: void step1(ref int x)... Are you saying all legacy C# code that uses that convention is now broken in 4.0, or did I misunderstand what you said? One thing I do have to do in 3.5 is initialize the int variable's value being passed. (Unless I use out instead of ref.) Something is rather goofy in VB.NET, I can pass a referential object as a by val field if I want. (It, of course, remains by ref.)
-
There hasn't been a good "why language X sucks and language Y is better" thread in a good while, so I thought I'd start one. Unlike most, however, this one has rules. I will post a reason C# is better than VB.NET and somebody reply with a reason VB.NET is better than C#. I (or somebody else) will then reply to that message stating another reason C# is better. And so on. Also, you must show code examples (when appropriate). I'll start. C# Is Less Verbose
Public Sub Something()
' VB.NET...
End Subpublic void Something()
{
// C#...
}You're turn (post why VB.NET is better than C#). :)
in C#:
i_can_do_this(); and_this(); //in the same line
Because of the delimeter, sometimes it makes sense not to waste one line and make the code more readable, especially when assigning multiple variables in the same context. -- Turns out you can do it in VB with ":" check the reply bellow.
modified on Tuesday, March 29, 2011 7:11 PM
-
Perhaps I should have said "C# Is More Concise". :rolleyes: But kudos to you for bringing up the counter point that VB.NET is more expressive. I think, however, that it's expressive in areas which do not require expressiveness. Maybe "End Sub" makes the code easier to read to somebody not initiated with the language, but it doesn't make the code any easier to write (you would have to know the "End Sub" in advance, so it's not expressing anything until you write it yourself). Of course, intellisense adds "End Whatever" for you, which brings me to my counter point: If-Statements Are Easier To Type in C# Supposing you have a code block and you want to surround it with an if-statement. In VB.NET, you must type "End If" in full:
If True Then
' Code Block.If you press ENTER after "Then", the "End If" will be added in the wrong place. Instead, you can go to the bottom of the code block and type "End If". In C#:
if (true)
{
// Code Block.All you have to type is "}" and the code auto-indents nicely. Along those same lines, C# waits until you type the closing brace... VB.NET is a little eager and indents the code before you need it to.
Now you are getting into intellisense (mis-)behavior. The difference in code behavior is basically C#: unless I tell you different, the next line is part of the same command VB: unless I tell you different, the next line is NOT part of the same command. "If" is one of those awkward step children that don't follow that rule very well. I agree with you, there is generally more typing in VB than in C#, whether or not you use VS to build your code. If I remember VB.NET correctly, you type If... and carriage return, it doesn't place the End If incorrectly, just awkwardly in relation to your cursor position and the commands you want to enter into the block.
-
There hasn't been a good "why language X sucks and language Y is better" thread in a good while, so I thought I'd start one. Unlike most, however, this one has rules. I will post a reason C# is better than VB.NET and somebody reply with a reason VB.NET is better than C#. I (or somebody else) will then reply to that message stating another reason C# is better. And so on. Also, you must show code examples (when appropriate). I'll start. C# Is Less Verbose
Public Sub Something()
' VB.NET...
End Subpublic void Something()
{
// C#...
}You're turn (post why VB.NET is better than C#). :)
VB has Explicit Event handlers VB:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
C#:
private void Form1_Load(object sender, EventArgs e)
{ }
This comes in real handy when you need the same event to handle multiple controls on a form.
Mike
-
VB is better because I use it more often, and have more experience with it than C#. Do I get points for honesty?
Michael K Gray wrote:
Do I get points for honesty?
I don't think so, at least in the rules set out. The rules kind of limit the playing field to people who have a fairly good knowledge of both. The fact that you can turn out code more quickly (and I assume accurately) in VB is offset by those who can say the same about C#. I wouldn't worry about it, this battle is all tongue-in-cheek(TIC). Your point just reinforces that everything said here has to be TIC.
-
in C#:
i_can_do_this(); and_this(); //in the same line
Because of the delimeter, sometimes it makes sense not to waste one line and make the code more readable, especially when assigning multiple variables in the same context. -- Turns out you can do it in VB with ":" check the reply bellow.
modified on Tuesday, March 29, 2011 7:11 PM
-
VB has a ":" colon to seperate multiple statements in the same line. statement1 : statement2
Mike
-
in C#:
i_can_do_this(); and_this(); //in the same line
Because of the delimeter, sometimes it makes sense not to waste one line and make the code more readable, especially when assigning multiple variables in the same context. -- Turns out you can do it in VB with ":" check the reply bellow.
modified on Tuesday, March 29, 2011 7:11 PM
-
Nishant Sivakumar wrote:
VB.NET supports by-ref extension methods. As of 4.0, C# does not.
WHAT!!! I don't have 4.0 version. This continues to work in 3.5: void step1(ref int x)... Are you saying all legacy C# code that uses that convention is now broken in 4.0, or did I misunderstand what you said? One thing I do have to do in 3.5 is initialize the int variable's value being passed. (Unless I use out instead of ref.) Something is rather goofy in VB.NET, I can pass a referential object as a by val field if I want. (It, of course, remains by ref.)
KP Lee wrote:
WHAT!!!
I don't have 4.0 version. This continues to work in 3.5: void step1(ref int x)...
Are you saying all legacy C# code that uses that convention is now broken in 4.0, or did I misunderstand what you said?You did misunderstand what I said :-) I did not mean the method arguments, I meant the source object. In VB the source object itself can be passed by reference.
Regards, Nish
Are you addicted to CP? If so, check this out: The Code Project Forum Analyzer : Find out how much of a life you don't have! My technology blog: voidnish.wordpress.com
-
VB has Explicit Event handlers VB:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
C#:
private void Form1_Load(object sender, EventArgs e)
{ }
This comes in real handy when you need the same event to handle multiple controls on a form.
Mike
obermd wrote:
VB has Explicit Event handlers
So??? You can handle multiple controls with the same function in C#. I have a 7X7 arrary of text boxes where the name is "n" plus the two index values converted to strings and appended. I happened to only need 9 events to go to the same function. I think you misstated this:
obermd wrote:
you need the same event to handle multiple controls
I think you meant "you need the same function to handle the same event in multiple controls" Personally I really like C# a lot better in this respect. This could just be because of my inexperience with VB, but I simply could not bind the events directly to the object in the array of text boxes, I had to create nine named fields, set them equal to the explicitly indexed box in the array, and manually list out all nine named fields with their event type in the function declaration. I shudder to think what the code would look like with all 49 events pointing to the same function. In C# each event delegates to the same function in a separate command, it has no direct tie to the function declaration. This is great, I just add the events directly from a do loop. I have no idea how to tie events to a function in a do loop in VB. (Just imagine the swearing I did when it wouldn't work, the frantic scrambling trying to find the documention of how multiple events to 1 function work in VB, then finding out specifically indexed controls in an array can't be tied to a function. ARRRGGGG.) OK, I had an advantage with C#. I took classes, that required books, and that mainly trained me to do some of this stuff.
-
VB has Explicit Event handlers VB:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
C#:
private void Form1_Load(object sender, EventArgs e)
{ }
This comes in real handy when you need the same event to handle multiple controls on a form.
Mike
C# and VB are actually pretty similar in this respect. In C#, the assignment of events is explicit, but it is usually "hidden" from you (in the designer.cs file). In fact, the designer.cs file is part of the reason partial classes exist.
-
Nope, 'cechode' wrote: General Re: Celebrity Deathmatch (VB.NET vs C#) Pin member cechode 23hrs 30mins ago i'm not a vb guy anymore but when i was i LOVED exit on first failed step. ( instead of nested if's ) BTW, I would use
bool s = step1() || step2() || step3();
to execute until first true result. -
i'm not a vb guy anymore but when i was i LOVED exit on first failed step. ( instead of nested if's )
Select Case False Case Step1() Case Step2() Case Step3() End Select
cant do that in C#
Did you show this VB6 code because you think it is a nice way of writing it? Why not write it like so: VB6: If Step1() Then If Step2() Then Step3() C# (more concise): if (Step1() && Step2()) Step3();
-
Oh sorry, it's so well known that I didn't think you'd need code to back it up :-) See this blog post: http://blog.gadodia.net/extension-methods-in-vbnet-and-c/[^]
Regards, Nish
Are you addicted to CP? If so, check this out: The Code Project Forum Analyzer : Find out how much of a life you don't have! My technology blog: voidnish.wordpress.com
Your argument does not work, because, as someone else suggested, in C# you would simply use a struct, not an enum. Check this for an example: Comparing enum flags in C# http://stackoverflow.com/questions/1086618/comparing-enum-flags-in-c
-
Your argument does not work, because, as someone else suggested, in C# you would simply use a struct, not an enum. Check this for an example: Comparing enum flags in C# http://stackoverflow.com/questions/1086618/comparing-enum-flags-in-c
Marc Greiner at home wrote:
Your argument does not work, because, as someone else suggested, in C# you would simply use a struct, not an enum.
Uhm, I think you are under a misunderstanding here. I am not arguing anything here :-) I did not write that blog entry. This thread is a fun-thread to talk about features (including useless ones) that exist in one language but not in another. VB allows ref extension methods (where the source is passed by ref), C# does not. I am not saying it has any utility whatsoever. I don't use VB. I use C++ (native) and C#. There may be cases where that is useful to have, but I am sure it's not a deal breaker. In future, please don't jump to assumptions about whether someone is making an argument when they are not! :)
Regards, Nish
Are you addicted to CP? If so, check this out: The Code Project Forum Analyzer : Find out how much of a life you don't have! My technology blog: voidnish.wordpress.com