C# Void 除了日志和 returns 真的不做任何其他事情吗?

C# Void doesn't do any other things except logs and returns true?

我对 SteamBot 代码有疑问。因此,如果有人将 "Bot" 添加为好友,那么它会在日志中显示添加者。它做到了这一点,之后我添加了更多代码,但那部分它就是不这样做。它只会记录某人添加的内容,并且看起来 returns 是真的。问题是,为什么它不执行代码的其他部分?

public override bool OnFriendAdd()
        {
            Bot.Log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) + " (" + OtherSID.ToString() + ") added me!");
            HttpWebRequest check =
            check.Method = "GET";
            HttpWebResponse checkResp;
            try
            {
                checkResp = check.GetResponse() as HttpWebResponse;
                StreamReader createRead = new StreamReader(checkResp.GetResponseStream());
                string resultCA = createRead.ReadToEnd();
                if (resultCA.Contains("success"))
                {
                    Regex SomeNameHere3 = new Regex("\"\" : \"([A-Za-z0-9]+)");
                    string swag3 = SomeNameHere3.Match(resultCA).Value;
                    swag3 = swag3.Replace("\"\" : \"", "");
                    currentSID = currentSID.ConvertToUInt64();
                    Log.Success("User: " + Bot.SteamFriends.GetFriendPersonaName(currentSID) + "  : " + swag3);
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                }
            }
            catch (WebException ex)
            {
                checkResp = ex.Response as HttpWebResponse;
                StreamReader createRead = new StreamReader(checkResp.GetResponseStream());
                string resultCA = createRead.ReadToEnd();
                if (resultCA.Contains("fail") && resultCA.Contains("Label does not exist"))
                {
                    HttpWebRequest create =
                    create.Method = "GET";
                    HttpWebResponse createResp = (HttpWebResponse)create.GetResponse();
                    StreamReader createSR = new StreamReader(createResp.GetResponseStream());
                    string createSRRTE = createSR.ReadToEnd();
                    if (createSRRTE.Contains("success"))
                    {
                        Regex SomeNameHere3 = new Regex("\"\" : \"([A-Za-z0-9]+)");
                        string get = SomeNameHere3.Match(createSRRTE).Value;
                        get = get.Replace("\"\" : \"", "");
                        Log.Success("User: " + Bot.SteamFriends.GetFriendPersonaName(currentSID) + "  : " + get);
                        Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                        Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                        Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                        Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                        Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                        Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                        Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                        Bot.SteamFriends.SendChatMessage(currentSID, type, ");
                       Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                       Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                       Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                   }
               }
           }
           return true;
       }

最好使用using语句,它会帮你处理

using (StreamReader createRead = new StreamReader(createResp.GetResponseStream()))
{
   ///
}

为了让 Stream 读取或写入,必须告知它正在读取或写入的非托管数据已完成,或者它不必继续读取或写入。

Read on why closing / disposing streams is necessary.

除了关闭流外,您还需要处理它。处理一个 Stream 会自动关闭它,所以只需要一个处理就足够了:

var stream = new FileStream();
// stuff;
stream.Dispose();

正如其他答案所建议的,您还可以选择在 IDisposable 对象(例如 Stream)上使用 using() {} 块。这将确保在 using 块的末尾处理 'used' 中的对象:

using(var stream = new FileStream()){
    //stuff;
}