Why doesn't it work?

dextra

New member
Joined
Mar 6, 2023
Messages
1
Programming Experience
Beginner
C#:
using System;
namespace UsingOut
{
  class Program
  {
    static string Whisper(string phrase, out bool whisper)
    {
whisper = true;
return phrase.ToLower();
    }
    static void Main(string[] args)
    {
      string statement = "AHH";
      Whisper(statement, out bool marker);
      Console.WriteLine(marker);
      Console.WriteLine(statement);
    }  
  
  }
}
I am new to C#, and I wrote this. It doesn't show any errors, and it doesn't come with lower characters. In fact, it just says "True AHH". I was wondering why. Can you tell me my mistake? Thanks in advance.
 
Last edited by a moderator:
Welcome to the forum. Next time, please post your code in code tags.

Anyway to answer your question, the code is working as written. You you are printing out statement on line 16. You don't do anything to change the string that statement is referencing. Recall that in C#, all strings are immutable. Notice that your line 9 returns a version of the string that is in lower case, but on line 14, you do nothing with that returned string.
 
Greetings! Welcome to the world of C#!
You were pretty close for a first try.
You just aren't using your static Whisper method :)
 
Is this an academic exercise? If so, after it's complete please avoid using static and out - in general they teach you nothing good and should almost never be used. By the time you know enough C# to know when they should properly be used, you'll generally avoid using them anyway. Educational courses generally start off by making everything static, so the C# acts like some simple scripting language or plain C, but it hobbles the entire object oriented paradigm around which the language is built, and by teaching you to express a preference for it, I believe it inhibits developing a good understanding of object orientation as a concept

How we might return two items of data from a method in the real world:

C#:
using System;
namespace NotUsingOut
{
  public class Program
  {
    public (string WhisperedStatement, bool Marker) Whisper(string phrase)
    {
      return (phrase.ToLower(), true);
    }
    static void Main(string[] args)
    {
      string statement = "AHH";
      var p = new Program();
      var whisperedStatementTuple = p.Whisper(statement);
      Console.WriteLine(whisperedStatementTuple.Marker);
      Console.WriteLine(whisperedStatementTuple.WhisperedStatement);
    }
 
  }
}

How we might return two items of data in an academic world, at an early level:


C#:
using System;
namespace NotUsingOutEither
{
  public class Program
  {
    public WhisperedStatement Whisper(string phrase)
    {
      return new WisperedStatement() { Statement = phrase.ToLower(),  Marker = true };
    }

    public static void Main(string[] args)
    {
      string statement = "AHH";
      var p = new Program();
      var whisperedStatement = p.Whisper(statement);
      Console.WriteLine(whisperedStatement.Marker);
      Console.WriteLine(whisperedStatement.WhisperedStatement);
    }
 
  }

  public class WhisperedStatement{
    public string Statement {get;set;} 
    public string Marker {get;set;}
  }
}
 
Last edited:
Just for completion's sake, the corrected static version of the function, that uses the return value from Whisper:

Static version of corrected function:
using System;

namespace UsingOut
{
    class Program
    {
        static string Whisper(string phrase, out bool whisper)
        {
            whisper = true;
            return phrase.ToLower();
        }

        static void Main(string[] args)
        {
            string statement = "AHH";
            string whisperedStatement = Whisper(statement, out bool marker);
            statement = whisperedStatement;
            Console.WriteLine(marker);
            Console.WriteLine(statement);
        }
    }
}
 
Back
Top Bottom