最急眼的一集

最近在学c#,看到这样一篇代码

using System;
using System.IO;

namespace BoilerEventAppl
{

    // boiler 类
    class Boiler
    {
        private int temp;
        private int pressure;
        public Boiler(int t, int p)
        {
            temp = t;
            pressure = p;
        }

        public int getTemp()
        {
            return temp;
        }
        public int getPressure()
        {
            return pressure;
        }
    }
    // 事件发布器
    class DelegateBoilerEvent
    {
        public delegate void BoilerLogHandler(string status);

        // 基于上面的委托定义事件
        public event BoilerLogHandler BoilerEventLog;

        public void LogProcess()
        {
            string remarks = "O. K";
            Boiler b = new Boiler(100, 12); // 创建一个Boiler对象,100温度 12 压力
            int t = b.getTemp(); // t是温度
            int p = b.getPressure(); // p是压力
            if (t > 150 || t < 80 || p < 12 || p > 15)
            {
                remarks = "Need Maintenance";
            }
            OnBoilerEventLog("Logging Info:\n");
            OnBoilerEventLog("Temparature " + t + "\nPressure: " + p);
            OnBoilerEventLog("\nMessage: " + remarks);
        }

        protected void OnBoilerEventLog(string message)
        {
            if (BoilerEventLog != null)
            {
                BoilerEventLog(message);
            }
        }
    }
    // 该类保留写入日志文件的条款
    class BoilerInfoLogger
    {
        FileStream fs;
        StreamWriter sw;
        public BoilerInfoLogger(string filename)
        {
            fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
            sw = new StreamWriter(fs);
        }
        public void Logger(string info)
        {
            sw.WriteLine(info);
        }
        public void Close()
        {
            sw.Close();
            fs.Close();
        }
    }
    // 事件订阅器
    public class RecordBoilerInfo
    {
        static void Logger(string info)
        {
            Console.WriteLine(info);
        }//end of Logger

        static void Main(string[] args)
        {
            BoilerInfoLogger filelog = new BoilerInfoLogger("d:\\boiler.txt");
            DelegateBoilerEvent boilerEvent = new DelegateBoilerEvent();
            boilerEvent.BoilerEventLog += new 
            DelegateBoilerEvent.BoilerLogHandler(Logger); // 打印到控制台的logger
            boilerEvent.BoilerEventLog += new    
            DelegateBoilerEvent.BoilerLogHandler(filelog.Logger); // 打印到文件的logger
            boilerEvent.LogProcess();
            Console.ReadLine();
            filelog.Close();
        }//end of main

    }//end of RecordBoilerInfo
}

我看了半天这个boilerEvent.BoilerEventLog += new ,没懂什么几把意思

然后突然想到这里没有分号,他是缩进!他是缩进!他是缩进!

其实应该是

boilerEvent.BoilerEventLog += new DelegateBoilerEvent.BoilerLogHandler(Logger);

太过分了,于是水一篇文

无独有偶,这网站还有一篇代码很逆天

using System;
using System.IO;

namespace FileApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // 创建一个 StreamReader 的实例来读取文件 
                // using 语句也能关闭 StreamReader
                using (StreamReader sr = new StreamReader("c:/jamaica.txt", Encoding.UTF8))
                {
                    string line;
                   
                    // 从文件读取并显示行,直到文件的末尾 
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            catch (Exception e)
            {
                // 向用户显示出错消息
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            Console.ReadKey();
        }
    }
}

这里如果你用microsoft visual studio 2022手动输入这一行
using (StreamReader sr = new StreamReader("c:/jamaica.txt", Encoding.UTF8))

就会出现编译错误的问题

我原文复制过去是可以编译通过的,但手打就不行,评论区有人说是编码问题

我测,太几把坑了