C#Hello world与失败

时间:2020-08-03 11:34:46

标签: c#

我正在尝试学习C#,并且尝试使用get和set设置字符串。但是,由于各种错误,它要么拒绝编译,要么由于堆栈溢出而崩溃

using System;
using System.Dynamic;
using System.Security.Cryptography.X509Certificates;

namespace CshEssentials
{
    class Program
    {
        public string a
        {
            get
            {
                Console.WriteLine($"Hello {a}"); return "0";
            }
            set
            {
                a = value;
            }
        }
        static void Main(string[] args)
        {
            a = Console.ReadLine();
        }

    }
}

2 个答案:

答案 0 :(得分:3)

您的代码有两个问题。

首先是您的Main函数是静态的。这意味着它是Program类型的方法,而不是Program实例的方法。您应该将属性设为静态,或确保在Program中实例化Main

第二,在您的setter中,您要再次设置属性,这将导致无限循环。

您可以按以下步骤解决这两个问题:

class Program
{
    // Create a backing field for the property
    private string a;

    // Conventionally properties start with a capital letter
    // (e.g. name is a private field, Name is a public property).
    public string A
    {
        get
        {
            // Unchanged, but now returns value based on the backing field
            Console.WriteLine($"Hello {a}"); return "0";
        }
        set
        {
            // Unchanged: now sets the backing field instead of calling the setter again
            a = value;
        }
    }

    static void Main(string[] args)
    {
        Program program = new Program(); // Create an instance
        program.A = Console.ReadLine();  // Set non-static property
    }

话虽这么说,让您的属性获取器执行有副作用,例如打印到控制台,这是未完成。通常,您可以让属性返回值,然后让调用者决定如何处理它:

class Program
{
    // Create a backing field for the property
    private string a;

    // Conventionally properties start with a capital letter
    // (e.g. name is a private field, Name is a public property).
    public string A
    {
        get
        {
            // Returns value based on the backing field,
            // caller can decide what to do with it
            return $"Hello {a}";
        }
        set
        {
            // Unchanged: now sets the backing field instead of calling the setter again
            a = value;
        }
    }

    static void Main(string[] args)
    {
        Program program = new Program(); // Create an instance
        program.A = Console.ReadLine();  // Set non-static property
        Console.WriteLine("program.A = {program.A}"); // Get and print
    }

答案 1 :(得分:0)

好吧,使用二传手,您正在创建一个无限循环。想象一下,a = Console.ReadLine();将用户输入发送到a中。这将调用setter方法,该方法以value的形式接收用户输入。您正在将其写回到a中,这将称为setter agein。等等等等。它不会停止,直到您遇到错误堆栈溢出。实际上,您的计算机无法无休止地调用该方法,因为在某些时候,没有剩余的内存,因此它将崩溃。

我建议您阅读有关吸气剂和吸气剂的手册。您的例子并不现实。

举一个简单的示例,说明如何使用getter和setter,请尝试以下方法:

using System;
using System.Dynamic;
using System.Security.Cryptography.X509Certificates;

namespace CshEssentials
{
    class Program
    {
        private static string aPrivate = "";

        public static string a
        {
            get
            {
                Console.WriteLine($"Getter for a was called. Returning {aPrivate}.");
                return aPrivate;
            }
            set
            {
                Console.WriteLine($"a was set to {value}.");
                aPrivate = value;
            }
        }
        static void Main(string[] args)
        {
            a = Console.ReadLine();
            Console.WriteLine(a);
        }

    }
}