FAQ: Method Output - Out

This community-built FAQ covers the “Out” exercise from the lesson “Method Output”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise Out

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Here’s my answer

using System;

namespace OutParameters
{
  class Program
  {
    static void Main(string[] args)
    {
      string ageAsString = "102";
      string nameAsString = "Granny";
      
      int ageAsInt;
      bool outcome;
      outcome = Int32.TryParse(ageAsString, out ageAsInt);
      
      Console.WriteLine(outcome);
      Console.WriteLine(ageAsInt);
      
      int nameAsInt;
      bool outcome2;
      outcome2 = Int32.TryParse(nameAsString, out nameAsInt);
      
      Console.WriteLine(outcome2);
      Console.WriteLine(nameAsInt);
    }  
  }
}
1 Like

So should Granny be null or 0 when printing
Console.WriteLine(nameAsInt);
?

int cannot be null unless made nullable (int?). So it will have to have a value that isn’t null. The default for int is 0, so it will be 0.

1 Like

Oh ok thanks. I’m guessing the narrative may be wrong for this section. It has example code saying:

int number2;
bool success2 = Int32.TryParse(" !!! ", out number2);
// number2 is null and success2 is false

but number 2 would be 0 not null right?

5 Likes

Yes, it would be 0 not null.

3 Likes

I came to this forum to report the exact same issue. the instructions need to be fixed as they give an incorrect description of Int32.TryParse().

From https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=netcore-3.1 , 0 is the return of a failed conversion, not null.

3 Likes

Right now I don’t understand why out is used and why is it good. Can you explain for me?

6 Likes

Someone explain me please this lesson. I don’t understand one thing in here. Now this course is about Methods only, but you guys write everything in Main().
I’m just siting all day (bathroom, bedroom, WC) and trying to figure out how to write everything in different methods cheatsheet also showing in diferent methods…, and call them from Main() and now I see that others just write everything in Main()… OH MY GOD :smiley:

1 Like

I’m confused. When methods use out is that always declared in the definition of the method, or is that something you throw in when you call the method?

And if it’s in the definition then why use out when you could just return a value?

Is out a way to prevent errors when a method is misused?

If I understand your question, I think you’re asking why use separate methods when everyone you see is just writing everything in Main().

The purpose of having separate methods is to make fixing, changing, and using code easier. When everything is mixed together it’s hard to find which pieces affect the rest.

Fixing: If you find a mistake in a big program it is hard to determine what changing this line here will do to the rest of the program. And is it really this line here or another line 100 lines up? Keeping things separate helps because it’s easier to see what effects what.

Changing: Let’s say after you write code you decide you want to make it do something new. Now you have to go in and change some variables and some outputs. Again, who is to say what will happen if you change this line here? Will this create problems later in the program? Having things separate means we can easier change this or that part and know the rest will still work the same.

Using Code: When you have a piece of code separated out into a method that you can call, you only have to write it once and can reuse it over and over with very little work. If you write everything in Main() then you have to rewrite or copy and paste your work to use it again. (This also makes it harder for you and others to read later.)

In conclusion, using methods outside of Main() makes your code easier to read, easier to fix errors, easier to change/update, and easier to use while you program. Very useful tool!

I am so lost in this exercise. I literally have no idea of what I am supposed to do and it feels mighty frustrating. I am ready to just give up and conclude this is not for me. I look at the solution and that does not help. I am stuck at the second part of the exercise. The solution does not have any part where “static public bool tryparse” is used but the hint gives that as the method signature, whatever that means. It just feels like someone is giving me instuctions in martian and I just hear sounds that make no sense.

2 Likes

Guys, can anyone simply explain what out does and how exactly do you use it? I tried to find a good explanation, but in documentation there is huge difficult explanation, and I haven’t found anything on youtube abt it but difference between ref and out, when I know neither of these keyword. Help me out please!

1 Like