简体   繁体   English

像属性一样的方法中变量的专有名称是什么?

[英]What is the proper name for variables in a method that acts like a property?

I know this is a stupid question, but I have been teaching myself c#, so im learning the lingo as I go. 我知道这是一个愚蠢的问题,但是我一直在自学c#,所以我在学习时学习了术语。

My question is: Often when I write methods, I find my self declaring objects and variables at the top of the method when I need them to share the same scope. 我的问题是:通常,当我编写方法时,当我需要对象共享相同的作用域时,我会在方法的顶部找到自声明对象和变量。 Do these types of objects/variables have a name? 这些类型的对象/变量是否有名称? I know when declared outside a method they would be properties. 我知道在方法外声明它们将是属性。

Example code of what I mean: My question is what to call the objects in the Question region. 我的意思的示例代码:我的问题是在“问题”区域中如何调用对象。

public Label start_Ping(String target, string name, ref bool router)
        {

       #region [ Question ] 
        Label status_Label = new Label(); //Declare the label which will be dynamically      created 

        Ping ping = new System.Net.NetworkInformation.Ping(); //Declare the ping object 

        PingReply reply = null; //Declare the pingReply object      


        byte[] buffer = new byte[100]; //Set the byte size 
       #endregion 

        if (name == "router")
        {
            try
            {
                reply = ping.Send(target, 50, buffer);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            if (reply.Status == IPStatus.Success)
            {
                router = true;
            }
            else
            {
                router = false;
                return null;
            }
        }
try
{
...

Thanks in advance. 提前致谢。 I understand this may be quite simple =) 我了解这可能很简单=)

In your code, status_Label , ping , reply and buffer are known simply as local variables . 在您的代码中, status_Labelpingreplybuffer简称为局部变量

That is, they're local to the method you create them in (or whatever scope you declare them in). 也就是说,它们对于您在其中创建它们的方法(或在其中声明它们的任何范围)都是局部的。

It doesn't matter where in your method you declare them, by the way, as long as they're within some scope. 这不要紧在你的方法,你声明它们,顺便说一句,只要他们是一些范围之内。

BoltClock is correct, but for future reference, variables declared outside of a method aren't properties. BoltClock是正确的,但为将来参考,在方法外部声明的变量不是属性。 They're called member variables. 它们称为成员变量。 Properties are different. 属性是不同的。

For example: 例如:

public class ExampleClass {
    String myString = "Hello World!";

    public String MyProperty {
        get { return myString; }
        set {
            myString = value;
            System.Console.WriteLine("MyProperty changed to " + value);
        }
    }
}

Here, myString is a member variable (or instance variable). 在这里,myString是成员变量(或实例变量)。 It can't be accessed from outside the class. 不能从班级外部访问它。

MyProperty, as the name suggests, is a property. 顾名思义,MyProperty是一个属性。 It's actually a kind of method that behaves like a variable, but allows other things to happen when its value is changed or accessed. 实际上,它是一种行为类似于变量的方法,但是当其值被更改或访问时,它允许发生其他事情。 In this example, setting MyProperty would output a message giving its new value. 在此示例中,设置MyProperty将输出一条给出其新值的消息。

There's some good documentation on this at MSDN here and here . 有一个在MSDN这方面的一些好的文档在这里这里

Hope this helps. 希望这可以帮助。

@BoltClock has answered your question; @BoltClock已回答您的问题; however, I wanted to point out that your statement, “when [variables are] declared outside a method they would be properties”, is incorrect. 但是,我想指出的是,“当[变量]在方法外声明为属性时,”是不正确的。 Variables declared outside a method are called fields . 在方法外部声明的变量称为字段 Properties typically have a similar purpose to fields (holding a value), but they need to be defined using get and set accessors, and can incorporate logic, such as validation or property change notification (which fields can't). 属性通常具有与字段(持有值)相似的目的,但是需要使用getset访问器对其进行定义,并且可以合并诸如验证或属性更改通知之类的逻辑(而字段则不能)。

For example: 例如:

public class Router
{
    private PingReply reply = null;

    public PingReply Reply
    {
        get { return reply; }
        set { reply = value; }
    }
}

In this case, reply is a field, whilst Reply is a property which gets or sets the value of reply . 在这种情况下, reply是一个字段,而Reply是一个获取或设置reply的值的属性。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM