| |

VerySource

 Forgot password?
 Register
Search
View: 1059|Reply: 3

After the object is created, does the attribute occupy memory space? Do attributes have to be associated with private fi

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-4 22:40:02
| Show all posts |Read mode
class Test
{
  public string name
  {
    get
      {
        return "NoName";
       }
    set
      {
       name = value;
       }
     
   }

  public test ()
  {
  }
}

class program
{
  static void Main (string [] args)
  {
    Test test1 = new Test ();
    Console.writeLine (test1.name);
   }
}

Accessing the name attribute in this way will cause an error? Master! Thanks! !!
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-4 23:51:01
| Show all posts
class Test
{
  public string name
  {
    get
      {
        return "NoName";
       }
     
   }

  public test ()
  {
  }
}

class program
{
  static void Main (string [] args)
  {
    Test test1 = new Test ();
    Console.writeLine (test1.name);
   }
}
Tried this
Reply

Use magic Report

0

Threads

60

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-1-5 06:21:01
| Show all posts
1. Do attributes have to be associated with private fields?

  Yes,

2. After the object is created, does the attribute occupy memory space?
  Of course, the property sets the method of saving or reading the private field.
The compiler actually compiles it into a special method
Called
Reply

Use magic Report

0

Threads

56

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-1-5 15:09:01
| Show all posts
Your Test class needs to have a field
set
{
    name = value;
}
It seems that writing like this will play with fire and self-immolation .. the dead loop -_- ///
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list