473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Learning OOP - why is string.Join static, while string.Spit is not?

I'm working on learning .NET and I'm curious about the reason for
using static methods in some cases. Specifically, the string class has
a split and a join method, but only the join method is static.

Both methods return a new string which is based on another string. It
would seem that both should not be static.

string mystring = "one, two, three, four";
string[] newstring = mystring.Split(',');

why not:

string[] mystring = new string[] {"one", "two", "three", "four"};
string newstring = mystring.Join(',');

I'd like to understand the underlying reason.

Thanks,
Epson
Jul 21 '05 #1
2 2064
"Epson Barnett" <go****@grider.org> wrote...
I'm working on learning .NET and I'm curious about the reason for
using static methods in some cases. Specifically, the string class has
a split and a join method, but only the join method is static.

Both methods return a new string which is based on another string. It
would seem that both should not be static.

string mystring = "one, two, three, four";
string[] newstring = mystring.Split(',');

why not:

string[] mystring = new string[] {"one", "two", "three", "four"};
string newstring = mystring.Join(',');

I'd like to understand the underlying reason.


Well I'll suggest it is because in your join example mystring isn't an
object of type String. It's a string array.

Split acts upon a string, so it is a method of a string. Join acts on a
string array and therefore it doesn't require a string to be defined. If it
did it would be an arbitrary instance of string used only to call the
method.

Tom
Jul 21 '05 #2
Epson Barnett <go****@grider.org> wrote:
I'm working on learning .NET and I'm curious about the reason for
using static methods in some cases. Specifically, the string class has
a split and a join method, but only the join method is static.

Both methods return a new string which is based on another string. It
would seem that both should not be static.

string mystring = "one, two, three, four";
string[] newstring = mystring.Split(',');

why not:

string[] mystring = new string[] {"one", "two", "three", "four"};
string newstring = mystring.Join(',');

I'd like to understand the underlying reason.


mystring here isn't a string, it's a string array - so you couldn't
have an instance method in string which worked that way. The only
single string parameter is the string which is used between each
element. In theory you could therefore have:

string newstring = ",".Join (mystring);

but that's hardly natural.

The thing is, when you descibe Join, you naturally talk about it
working "on" a collection of strings. When you describe Split, you
naturally talk about it working "on" a single string - it's therefore
appropriate for Split to be an instance method, but Join not to be.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
17
by: Tom | last post by:
Is there such a thing as a CONTAINS for a string variable in VB.NET? For instance, I want to do something like the following: If strTest Contains ("A","B", "C") Then Debug.WriteLine("Found...
2
by: Epson Barnett | last post by:
I'm working on learning .NET and I'm curious about the reason for using static methods in some cases. Specifically, the string class has a split and a join method, but only the join method is...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
7
by: Malcolm | last post by:
This is a program to convert a text file to a C string. It is offered as a service to the comp.lang.c community. Originally I thought it would be a five minute job to program. In fact there are...
4
by: neptundancer | last post by:
Hi, to extend my skills, I am learning python. I have written small program which computes math expression like "1+2*sin(y^10)/cos(x*y)" and similar, so far only + - * / ^ sin con tan sqrt are...
12
by: Ed | last post by:
Hi All, The sample code below doesn't work, but if you take out the Question Mark character, "?" in front of the value "Print" of the "action" attribute, it does work! Does anyone here know...
9
by: gs | last post by:
is there any built in function or dotnet framework(version 2) to merge a generic list of string into one string with each element delimited by specified delimiting string? or do I have to roll...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.