Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9248987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:05:00+00:00 2026-06-18T10:05:00+00:00

To give an example, I have seen a lot of C# code such as

  • 0

To give an example, I have seen a lot of C# code such as the following:

XmlNode n = GetXmlNode();
String[] attributes = new String[n.Attributes.Count];
for (int x = 0; x < n.Attributes.Count; x++)
{
    //do something
}

Now, if this were java, we could write code similar to the following, but we would be guilty of calling the getAttributes() method twice, and if I am not mistaken there is a rule that says that instead of calling the same method multiple times, just declare a variable to hold a reference to the object returned by the method call and then use that variable as many times as necessary.

Node n = GetXmlNode();
String[] attributes = new String[n.getAttributes().getLength()];
for (int x = 0; x < n.getAttributes().getLength(); x++)
{
    //do something
}

But since a C# property is just a getter method and a setter method encapsulated in one type member, does it follow that the same rule should be observed?

Or does the rule not apply in this case because it is “safe” to assume that calls to C# properties and Java get methods in standard libraries just return references to fields rather than perform intense labor?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:05:01+00:00Added an answer on June 18, 2026 at 10:05 am

    It depends if there is some computation to get the value of property (I consider doing some larger computation in property getter as bad practice, but you can’t rely on that there is no such property).

    The rule is: In most of the cases you will be ok calling property multiple times. …but who knows, who implemented that god damn performance killer property, just because it was available to do it in that way….

    Good – automatic property

    public string MyValueProperty { get; set; }
    

    is compiled to getter/setter like in java:

    private string myValueProperty;
    public string MyValueProperty
    {
         get{
              return myValueProperty;
         }
         set{
              this.myValueProperty = value;
         }
    }
    

    You will get little or no performance hit calling such property multiple times. It is similar to calling basic getter/setter methods in java.

    Bad – some computing is involved to get value of property

    public string MyLongTimeToGetValueProperty
    {
         get 
         { 
              var res = DoSomeComputation();
              return res;
         }
    }
    

    It is better to avoid calling such properties multiple times. (Regardless that such kind of properties should be refactored to methods, because they behave like one)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please give me some examples of jump table usage. I have seen this example
Can anybody give me a little advice please? I have a string, for example
I'm looking to remove duplicate phrases from any give string. Example: My_First_Post_My_First_Post.htm Would have
Example: I have a selector like this, which I give to another method as
i will give an example of the problem i have. My XML is like
I know how to do this... I'll give example code below. But I can't
I have seen a lot of examples, but I am not sure how it
I have seen this on some survey websites. What is the C# code they
I am new to php and i have seen rather Ambiguous function name convention
In My application i want to flip the view.. I have seen such animation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.