Like Tree3Likes
  • 1 Post By Magos
  • 1 Post By nvoigt
  • 1 Post By Mario F.

Why return type? from a method?

This is a discussion on Why return type? from a method? within the C# Programming forums, part of the General Programming Boards category; I'm looking at some C# intro stuff , and they say you can return a value from a method, and ...

  1. #1
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Why return type? from a method?

    I'm looking at some C# intro stuff, and they say you can return a value from a method, and that you can specify ?:
    Code:
    class Foo
    {
        byte? Bar()
        {
             return 0xFF;
        }
    }
    Normally, from my understanding, you use ? to say the variable can be set to null (but apparently you can't use it on strings, even though they didn't say why):
    Code:
    int? foo = null;
    So why am I specifying a ? on a method value? To say that I might be returning null, or so that I can assign my return value to something previously specified as having ??
    Code:
    byte? b;
    
    b = Foo.Bar();
    They're not clear on why I would want to be able to add a ? to my method's return type.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    string is a reference type and can already be assigned null (no need for ?). ? is only used on value types.
    ? is syntactic sugar around System.Nullable<T> boxing.
    quzah likes this.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So on a method's returned value type, is it to indicate that I might return null, or so that I can assign my value to something declared as type?? The latter doesn't make sense to me, but neither does not having to free anything.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    In .NET you have two types of... types. Value types and Reference types. All classes are reference types, which means that whenever you do "MyClass m;" you've got a reference to a MyClass instance that is set to NULL. Accessing members of m throws a null reference exception. All primitives and all structs (big difference to C++) are value types. That means if you do "int i;" you actually have an int named i, not a reference to an int set to null. You can access i and all it's methods.

    Pass by reference/value is automatically for those types. Classes are always passed by ref, while structs and primitives are always passed by value (although you can override this behaviour by keywords).

    "System.String s;" is equivalent to "std::string* s = NULL;" in C++
    "System.Drawing.Point p;" is equivalent to "Point p;" in C++

    Because Point in .NET is a struct and String is a class.

    So an int can (as in C++) never be null, because it's a value type. Only reference types can be null.

    Obviously, there are cases where you need a value to be an int or be null. For example the contents of a database field in your program. So prior to Generics in .NET, people did the only thing possible: they wrote their own class "MyInt" that held a single int and now they could return "MyInt" because it would either be null or hold a value. Without templates, that sucks. You don't want to write 20 classes so you can return nullable primitives of all kind. In C++ you'd probably use a pointer or write a single template class for all kinds of primitives.

    When generics where intoduced, a class called System.Nullable<> was delivered, that did exactly that: provide a generic way to have a nullable primitive.

    T? is just a compiler shortcut for System.Nullable<T> and only works for primitives (again, including structs, big difference to C++) because classes where already nullable from day 1.

    I'd say as long as you don't need it, stay away from System.Nullable<>, as soon as one of your int-functions needs to return null, it will become obvious
    quzah likes this.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nvoigt View Post
    T? is just a compiler shortcut for System.Nullable<T> and only works for primitives (again, including structs, big difference to C++) because classes where already nullable from day 1.

    I'd say as long as you don't need it, stay away from System.Nullable<>, as soon as one of your int-functions needs to return null, it will become obvious
    So when used in next to a return type, it's just indicating that this function may be returning null then?
    Code:
    class Foo
    {
        byte? Bar()
        {
            ...
        }
    }
    And without the ? there you can't return null (even though it's a primitive type and you generally don't need to) then? They just didn't clarify why I would ever want to use ? on my method's return type. They just said I could, without saying why I would ever want to, or what that even indicated.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    Yup. It's just making sure you give the right type to the return value, because the ? isn't just signaling a nullable type, it actually defines a different type.

    On this case, it's an alias to System.Nullable<byte>. It's required because testing for null on value types generates a compile time error.
    quzah likes this.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Thanks guys.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    Quote Originally Posted by Magos View Post
    string is a reference type and can already be assigned null (no need for ?)
    I'd just like to point out that while String is s reference type it is treated differently (than the other reference types) in C#. It's immutable (value types are generally immutable), the '==' operator does an actual comparison of the strings, instead of the normal comparison of references that reference types use (value types should compare actual internal values with '==' to be consistent with .NET).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Depending on Template Method Return Type
    By M.Richard Tober in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2011, 09:34 PM
  2. return struct from asp.net web service method
    By Elkvis in forum C# Programming
    Replies: 1
    Last Post: 02-09-2009, 07:39 PM
  3. which one run first, destructor or return method
    By sleith in forum C++ Programming
    Replies: 17
    Last Post: 01-09-2009, 12:27 PM
  4. Replies: 6
    Last Post: 04-09-2006, 04:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21