none
Code problem CSO161. RRS feed

  • Question

  • The code giving me the problem is:

    static int Main(string[] args)

    My book said I must have windows operating system on but I don't know how. I require help figuring out how to make it work.

    Wednesday, January 8, 2020 12:06 AM

Answers

  • CSO161 means "Not all code paths return a value".

    If you declare your main procedure with return type int, you have to put "return something;" near the end. (say, "return 0;")

    Alternatively, you can change the procedure signature to the following:

    static void Main(string[] args)

    Both way will do.


    Wednesday, January 8, 2020 1:18 AM
    Answerer
  • Hi Blue_Crayon,

    Thank you for posting here.

    My reply is a little extension of cheong00's.

    If there are branch statements such as "if" in the method, ”return“ is required in each branch statement.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 8, 2020 1:54 AM

All replies

  • CSO161 means "Not all code paths return a value".

    If you declare your main procedure with return type int, you have to put "return something;" near the end. (say, "return 0;")

    Alternatively, you can change the procedure signature to the following:

    static void Main(string[] args)

    Both way will do.


    Wednesday, January 8, 2020 1:18 AM
    Answerer
  • Hi Blue_Crayon,

    Thank you for posting here.

    My reply is a little extension of cheong00's.

    If there are branch statements such as "if" in the method, ”return“ is required in each branch statement.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 8, 2020 1:54 AM
  • Hi Blue_Crayon,

    Has your issue been resolved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 14, 2020 9:28 AM
  • Another option is to use Environment.Exit, pass in a int which is returned to the operating system.

    Simple example where if the file exists the return code is 0 while if the file does not exists exit with 1. Then the calling app reacts accordingly. This works for WinForm apps and console apps.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var fileName = "SomeFile.txt";
                if (File.Exists(fileName))
                {
                    var lines = File.ReadAllLines(fileName);
                    // do some work
                    Environment.Exit(0);
                }
                else
                {
                    Environment.Exit(1);
                }
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Tuesday, January 14, 2020 10:28 AM
    Moderator