Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 224601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:19:55+00:00 2026-05-11T19:19:55+00:00

This is related to conventions used in C#. I’ve got a method that has

  • 0

This is related to conventions used in C#.

I’ve got a method that has two parameters (X and Y coordinates). These coordinates represent the position at which a “tile” may reside. If a tile resides at these coordinates, the method returns its number. If no tile resides at these coordinates, I’m wondering how the method should behave.

I see three options:

  1. Use exceptions. I may raise an exception every time Method finds no tile. However, as this situation is not rare, this option is the worst one.
  2. Do it the old fashioned C++ way and return -1 if there is no tile.
  3. Make the tile number a reference parameter and change the return type of method to boolean to show whether there is a tile or not. But this seems a bit complicated to me.

So, what should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:19:56+00:00Added an answer on May 11, 2026 at 7:19 pm

    Return -1.

    This is not just a C++ convention, it’s also common in the .NET Framework – e.g. methods like String.IndexOf or properties like SelectedIndex for controls that represent lists.

    EDIT

    Just to elaborate, of the three options in your question (Exception, return -1, out parameter), returning -1 is the way to go. Exceptions are for exceptional situations, and the Microsoft coding guidelines recommends avoiding out parameters where possible.

    In my view returning -1 (provided it’s always going to be an invalid value), returning a nullable int, or returning a Tile object are all acceptable solutions, and you should choose whichever is most consistent with the rest of your app. I can’t imagine any developer would have the slightest difficulty with any of the following:

    int tileNumber = GetTile(x,y);
    if  (tileNumber != -1)
    {
       ... use tileNumber ...
    }
    
    
    int? result = GetTile(x,y);
    if (result.HasValue)
    {
        int tileNumber = result.Value; 
       ... use tileNumber ...
    }
    
    
    Tile tile = GetTile(x,y);
    if (tile != null)
    {
       ... use tile ...
    }
    

    I’m not sure I understand Peter Ruderman’s comment about using an int being “much more efficient than returning a nullable type”. I’d have thought any difference would be negligible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is related to a chapter from beautiful code . And in that chapter
The answers to this related question of mine lead me to choose Java for
This is related to the accepted answer for What’s your #1 way to be
This is related to the this question and the answer maybe the same but
This is related to some other questions, such as: this , and some of
(This is related to Floor a date in SQL server .) Does a deterministic
This is related to a question I asked the other day on how to
This is related to this question . I'm writing a Flex app (a WindowedApplication)
This is related to my earlier question , but I thought I'd simplify it
This is related to my question on how to handle errors from jQuery AJAX

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.