Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Errrhhh ..... CSharp or VB
Message
From
26/09/2011 10:00:17
 
 
To
26/09/2011 09:37:28
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, United States
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01524423
Message ID:
01524723
Views:
41
>>>>>>>>MyName
>>>>>>>>myName
>>>>>>>>_myName
>>>>>>>>this.MyName
>>>>>>>>
>>>>>>>>are all valid names but the casing tells you the scope of the variable.
>>>>>>>
>>>>>>>Please go on. I sincerely am in learning mode here. What is the scope of each of those?
>>>>>>
>>>>>>Here's a sample, I wouldn't do this as a rule, but it illustrates the naming by case.
>>>>>>
>>>>>>Field variables i.e. _myName are global to the class in which they are declared but since private are not available outside of class
>>>>>>Properties (public) are global to the class in which they are declared.and are available to any thing which consumes this class
>>>>>>Parameters use camel case and are limited in scope to the method
>>>>>>
>>>>>>
>>>>>>
>>>>>> public class MyClass
>>>>>>    {
>>>>>>        private string _myName;
>>>>>>        public string MyName { get; set; }
>>>>>>
>>>>>>        public MyClass(string myName)
>>>>>>        {
>>>>>>            _myName = myName;
>>>>>>
>>>>>>            this.GetNewName(myName);
>>>>>>        }
>>>>>>
>>>>>>        private void GetNewName(string myName)
>>>>>>        {
>>>>>>            this.MyName = _myName + ":" + myName;
>>>>>>        }
>>>>>>    }
>>>>>>
>>>>>
>>>>>This is exactly how I do it today, after playing around with different guidelines that last several years.
>>>>>
>>>>>Other picky points with me: when people do explicity scope _myName to private
>>>>>Not using Auto-Implemented Properties
>>>>>
>>>>>Question: I've always been confused about how the compiler optimizes string concatonation. Should you be using String.Format or String.Concat instead of the way you're doing it? Or will the compiler optimize it?
>>>>
>>>>
>>>>For short strings, as in the demo, i don't think it matters.... but for the most part I use stringbuilder to concat long strings of data.....
>>>
>>>Another newbie question -- isn't StringBuilder a bazillion times faster than String?
>>
>>AFAIK, there's no point in using a StringBuilder for something like this:
string s = "Tom" + "Dick"+"Harry"+"AsManyMoreAsYouLike"
since you are only assigning to a string once. But, since strings are immutable, if you do this:
string s = "Tom";
>>s += "Dick";
>>s += "Harry";
then you are, in effect, creating a new 's' and discarding the old one with every addition. In that situation using a StingBuilder comes into its own....
>
>Ahhh, that makes sense. It's easy to have your spider sense go off when you see plus signs and strings, so I usually just use String.Format or String.Concat (or StringBuilder for larger cases) to avoid it.

I was just thinking how often I see someone not using StringBuilder at all or, if they do, not taking advantage of the fluent interface. eg. I see this:
string result = "Hello to Fred Bloggs";
            result = result.Insert(14, "J. ");
            result = result.Replace("Fred", "Humphrey");
rather than:
string result = new StringBuilder("Hello to Fred Bloggs")
                .Insert(14, "J. ")
                .Replace("Fred", "Humphrey")
                .ToString();
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform