简体   繁体   English

C#无法从void转换为bool - CS1503

[英]C# Cannot convert from void to bool - CS1503

I am writing a small program to act as a stopwatch as part of a course. 我正在写一个小程序作为课程的一部分作为秒表。 The issue I am having is that when I try to compile I am getting Cannot convert from void to bool on my Duration() method in my Program.cs class. 我遇到的问题是,当我尝试编译时,我的Program.cs类中的Duration()方法Cannot convert from void to bool The method should return TimeSpan 该方法应返回TimeSpan

I can't see where it is being set to void . 我无法看到它被设置为void Maybe something lower level in the C# runtime? 可能是C#运行时中较低级别的东西? Not sure. 不确定。

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Stopwatch
{
    class Program
    {
        static void Main(string[] args)
        {
            var stopwatch = new StopWatchController();
            stopwatch.Start();
            stopwatch.Start(); // Second Start() method should throw an exception
            stopwatch.Stop();
            Console.WriteLine(stopwatch.Duration()); // Error appears here
        }
    }
}

StopwatchController.cs

using System;
using System.Runtime.CompilerServices;

namespace Stopwatch
{
    public class StopWatchController
    {
        private DateTime _startTime;
        private DateTime _finishTime;
        private TimeSpan _duration;
        private bool _isWatchRunning;

        public void Start()
        {
            if (!_isWatchRunning)
            {
                _isWatchRunning = !_isWatchRunning;
                _startTime = DateTime.Now;
            }
            else
            {
                throw new Exception("InvalidArgumentException");
            }
        }

        public void Stop()
        {
            _isWatchRunning = false;
            _finishTime = DateTime.Now;
        }

        public void Duration() // I'm an idiot
        {
            _duration = _finishTime - _startTime;
        }
    }
}

Duration should return TimeSpan to be used in Console.WriteLine : Duration应返回要在Console.WriteLine使用的TimeSpan

    public TimeSpan Duration()
    {
        return _duration = _finishTime - _startTime;
    }

    ...

    Console.WriteLine(stopwatch.Duration()); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决此错误“错误 CS1503:参数 1:无法从‘void’转换为‘bool’” - How to resolve this error "error CS1503: Argument 1: cannot convert from 'void' to 'bool'" Console.WriteLine() 错误 CS1503 无法从“void”转换为“bool” - Console.WriteLine() error CS1503 cannot convert from 'void' to 'bool' C# 错误消息:CS1503Argument 1:无法从“void”转换为“bool” - C# Error Message: CS1503Argument 1: cannot convert from 'void' to 'bool' CS1503 C#参数1:无法从“ System.IO.Stream”转换为“ char []” - CS1503 C# Argument 1: cannot convert from 'System.IO.Stream' to 'char[]' CS1503:参数 2:无法从 'System.Action[*,*,*] 转换为 System.Action[]' C# 2022 - CS1503: Argument 2: Cannot convert from 'System.Action[*,*,*] to System.Action[]' C# 2022 C# 错误 CS1503 参数 1:无法从“字符串”转换为“字符” - C# Error CS1503 Argument 1: cannot convert from 'string' to 'Character' CS1503 C# 参数 1:无法从“字符串”转换为“System.Data.SqlClient.SqlCommand” - CS1503 C# Argument 1: cannot convert from 'string' to 'System.Data.SqlClient.SqlCommand' C# - CS1503 - 参数 1:无法从“字符串”转换为“整数” - C# - CS1503 - Argument 1: cannot convert from 'string' to 'int' CS1503:参数 1:无法从 'int' 转换为 'byte' C# - CS1503: Argument 1: cannot convert from 'int' to 'byte' C# CS1503:参数 1:无法从“方法组”转换为“Action,bool>” - CS1503: Argument 1: cannot convert from 'method group' to 'Action,bool>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM