Click here to Skip to main content
15,915,172 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
class Program
{
    static void Main(string[] args)
    {

        string str = "Hello";


        for (int i = 0; i < str.Length; i++)
        {
            Console.WriteLine(str[i]);

        }
    }
}


In the above program, I have used Length property, I dont want to use string Length property, i want to create a similar property like Length. What is the logic and how can i achieve it?
Posted
Updated 7-Jan-16 9:28am
v2
Comments
Dave Kreskowiak 7-Jan-16 15:46pm    
That doesn't make sense. It's a property because it's, well, a property of a string instance, how long the string is.

Property of Length? Of what? and why would you want to do this?
Aditakumar2311 8-Jan-16 1:20am    
Thanks. Can i see in .net how this 'Length' property is coded?

can i make the same program without using Length property?
Dave Kreskowiak 8-Jan-16 8:21am    
You can but it's not what a normal piece of Property code would look like and really isn't going to do you any good at all.

Reference Source[^]

Learn what Properties are and how they are used before you go spelunking through the .NET Reference Source.
Sergey Alexandrovich Kryukov 7-Jan-16 15:47pm    
You can read about properties anywhere. What's the problem? What do you want to achieve?
What have you tried so far?
—SA
Aditakumar2311 8-Jan-16 1:22am    
Thanks. Can i see in .net how this 'Length' property is coded?

can i make the same program without using Length property?

1 solution

Properties are very basic stuff and you'll be able to read up on that in any C#-tutorial on the web or in any introductory book on C#. Here's a (legal) free PDF book on C#: Introduction to Programming with C#[^]

However, I'll add that it'll depend on whether you want to add a property to a class that you wrote yourself or to a class from the .Net Framework. The latter case is not possible because there are no true extension properties in C# yet. Only workarounds that achieve somewhat the same goal but don't work the same way. You'll find them by googling for "C# extension properties".
 
Share this answer
 
Comments
Aditakumar2311 8-Jan-16 7:43am    
Thanks
Sascha Lefèvre 8-Jan-16 7:57am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900