Simple C# Question about Void Routines

seancampbellseancampbell Pennsylvania, USA
Hi guys, I am a VB.Net developer (i post in the VB.net forum) teaching myself to do work in C#. In Vb.Net I use Exit Sub or Exit Function quite often, I could not find an equivalent in C#. I read a couple of posts on other sites where people instructed the user to do a 'return;'.

If I am doing a Void, why would I want to call a return? Is there another method, or is this the accepted equivalent?

Comments

  • : Hi guys, I am a VB.Net developer (i post in the VB.net forum)
    : teaching myself to do work in C#. In Vb.Net I use Exit Sub or Exit
    : Function quite often, I could not find an equivalent in C#. I read a
    : couple of posts on other sites where people instructed the user to
    : do a 'return;'.
    :
    : If I am doing a Void, why would I want to call a return? Is there
    : another method, or is this the accepted equivalent?

    You do not need to include a return statement in a void method. The 'return;' call you've seen is relevant only where you want to exit the middle of a function.

    Here are examples of each:

    [code]
    //print 0 through 9 and exit when complete
    public void DoSomething()
    {
    for(int i=0; i<10; i++)
    {
    Console.WriteLine(i);
    }
    }

    //print 0 through 5 and exit
    public void DoSomethingElse()
    {
    for(int i=0; i<10; i++)
    {
    Console.WriteLine(i);
    if(i == 5)
    {
    return;
    }
    }
    }
    [/code]
  • seancampbellseancampbell Pennsylvania, USA
    Thanks... I was simply just trying to see what the accepted solution for Exit Function and Exit Sub (from VB.Net) was in C#. I read an article that said that Vb.Net's Exit Function and Exit Sub functions are deprecated and in VB I should use Return Nothing as well... Go figure, I have to start programming in C# to find out I'm doing it wrong... FAIL

    -Sean_C
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories