强制函数返回值,使编译错误C# [英] Force function to return value and make compile error C#

查看:223
本文介绍了强制函数返回值,使编译错误C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,这个类中的方法。然而,这种类方法返回一个字符串。当我调用类方法,我没有得到一个错误eventhough即时通讯不醒目的字符串值回报。有没有一种方法,我可以让C#和.NET逼我返回一个值时,捕捉值。 这里是我的意思的例子。

I have a class and a method within that class. However this class method returns a string. When i call the class method i dont get an error eventhough im not catching the string value return. Is there a way that I can make C# and .net force me to capture the value when returning a value. Here is an example of what i mean.

1 - 创建一个类的测试。

1- create a class test.

class test
    {

        public string mystring() {

            return "BLAH";

        }
    }

2 - 调用类的方法,从程序或其他类

2- call the class method from program or another class

test mystring = new test();
mystring.mystring();

我的编译器,同时在Visual Studio中工作不抱怨,我不是捕捉返回值。 这是正常的还是我失去了一些东西。我可以强制编译器,函数返回值,但我没有赶上它来通知我?

my compiler while working in visual studio does not complaint that I'm not capturing the return value. is this normal or I'm missing something. can I force the compiler to notify me that the function returns a value but I'm not catching it?

在此先感谢您的任何建议,你可能有。

Thanks in advance for any suggestions you may have.

推荐答案

您可以将其转换为一个属性,而不是一个方法:

You could convert this to a property instead of a method:

  public string myString
  {
    get
    {
      return "Blah";
    }
  }

然后就无法编译,如果你简单地调用属性:

Then you can't compile if you simply call the property:

myString.myString; //Results in "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement" Error

这篇关于强制函数返回值,使编译错误C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆