Upload
amoy-pixel-nicholson
View
213
Download
0
Embed Size (px)
Citation preview
8/12/2019 C# String Array
1/6
C# Classes
C# Csharp
VB Net
http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-4712093147740724&output=html&h=90&slotname=1737246071&adk=3576056032&w=200&lmt=1378587238&flash=11.8.800&url=http%3A%2F%2Fwww.dotnetperls.com%2Fstring-array&dt=1379450355818&bpp=11&bdt=564&shv=r20130912&cbv=r20130906&saldr=sa&prev_slotnames=6682575170%2C4020807141%2C7895882516%2C2814286994&correlator=1379450356439&frm=20&ga_vid=1253452052.1379450357&ga_sid=1379450357&ga_hid=864017601&ga_fc=0&u_tz=-300&u_his=6&u_java=1&u_h=768&u_w=1280&u_ah=728&u_aw=1280&u_cd=32&u_nplug=10&u_nmime=20&dff=verdana&dfs=18&adx=812&ady=603&biw=1263&bih=600&oid=3&ref=https%3A%2F%2Fwww.google.com.jm%2F&vis=2&fu=0&ifi=5&dtd=7935&xpc=suiOVshqJl&p=http%3A//www.dotnetperls.com&rl_rc=true&adsense_enabled=true&ad_type=text&ui=rc:0&oe=utf8&height=90&width=200&format=fpkc_al_lp&fs=large&f=verdana&kw_type=radlink&prev_fmts=200x90_0ads_al&rt=ChBSOL37AA5bKwo0KpFTADaCEgZWQiBOZXQaCB6hpcChXp3AIN-1zyAoATACSN-1zyBSEwjHzbKHpdO5AhWFLTQKHaZEADo&hl=en&kw0=C%23+Classes&kw1=C%23+Csharp&kw2=VB+Net&okw=VB+Nethttp://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-4712093147740724&output=html&h=90&slotname=1737246071&adk=3576056032&w=200&lmt=1378587238&flash=11.8.800&url=http%3A%2F%2Fwww.dotnetperls.com%2Fstring-array&dt=1379450355818&bpp=11&bdt=564&shv=r20130912&cbv=r20130906&saldr=sa&prev_slotnames=6682575170%2C4020807141%2C7895882516%2C2814286994&correlator=1379450356439&frm=20&ga_vid=1253452052.1379450357&ga_sid=1379450357&ga_hid=864017601&ga_fc=0&u_tz=-300&u_his=6&u_java=1&u_h=768&u_w=1280&u_ah=728&u_aw=1280&u_cd=32&u_nplug=10&u_nmime=20&dff=verdana&dfs=18&adx=812&ady=603&biw=1263&bih=600&oid=3&ref=https%3A%2F%2Fwww.google.com.jm%2F&vis=2&fu=0&ifi=5&dtd=7935&xpc=suiOVshqJl&p=http%3A//www.dotnetperls.com&rl_rc=true&adsense_enabled=true&ad_type=text&ui=rc:0&oe=utf8&height=90&width=200&format=fpkc_al_lp&fs=large&f=verdana&kw_type=radlink&prev_fmts=200x90_0ads_al&rt=ChBSOL37AA5bJgo0KpFTADaCEglDIyBDc2hhcnAaCKibq7cTxI_1IN-1zyAoATACSN-1zyBSEwjHzbKHpdO5AhWFLTQKHaZEADo&hl=en&kw0=C%23+Classes&kw1=C%23+Csharp&kw2=VB+Net&okw=C%23+Csharphttp://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-4712093147740724&output=html&h=90&slotname=1737246071&adk=3576056032&w=200&lmt=1378587238&flash=11.8.800&url=http%3A%2F%2Fwww.dotnetperls.com%2Fstring-array&dt=1379450355818&bpp=11&bdt=564&shv=r20130912&cbv=r20130906&saldr=sa&prev_slotnames=6682575170%2C4020807141%2C7895882516%2C2814286994&correlator=1379450356439&frm=20&ga_vid=1253452052.1379450357&ga_sid=1379450357&ga_hid=864017601&ga_fc=0&u_tz=-300&u_his=6&u_java=1&u_h=768&u_w=1280&u_ah=728&u_aw=1280&u_cd=32&u_nplug=10&u_nmime=20&dff=verdana&dfs=18&adx=812&ady=603&biw=1263&bih=600&oid=3&ref=https%3A%2F%2Fwww.google.com.jm%2F&vis=2&fu=0&ifi=5&dtd=7935&xpc=suiOVshqJl&p=http%3A//www.dotnetperls.com&rl_rc=true&adsense_enabled=true&ad_type=text&ui=rc:0&oe=utf8&height=90&width=200&format=fpkc_al_lp&fs=large&f=verdana&kw_type=radlink&prev_fmts=200x90_0ads_al&rt=ChBSOL37AA5bFwo0KpFTADaCEgpDIyBDbGFzc2VzGgjoxBmugvOkDiDftc8gKAEwAkjftc8gUhMIx82yh6XTuQIVhS00Ch2mRAA6&hl=en&kw0=C%23+Classes&kw1=C%23+Csharp&kw2=VB+Net&okw=C%23+Classeshttp://www.google.com/url?ct=abg&q=https://www.google.com/adsense/support/bin/request.py%3Fcontact%3Dabg_afc%26url%3Dhttp://www.dotnetperls.com/string-array%26gl%3DJM%26hl%3Den%26client%3Dca-pub-4712093147740724&usg=AFQjCNFx1N-NTsG-cB6cd-fw_pxEr3J2Uw8/12/2019 C# String Array
2/6
Result
Four string arrays are initialized.
The above Main
functionshows four
string arrays, each
equivalent to thecompiler. The biggest difference is that the first
three arrays are declared on one line, while
the fourth array is assigned in separate
statements.
Tip:
The fourth array would allow you to test
each value or insert logic as you assign it.
This is sometimes useful.
The above
initializationseach
result in several
intermediate language
instructions. To create the
array reference, "newarr
string" is emitted. Andthen to assign each
element the instruction
"stelem" is used.
newarr Instruction
stelem Instruction
Fields
Next we use string arrays as fields orproperties in classes. This is useful for storing
values, either statically or in instances. You can
also return string arrays, with methods or
indexers.
Next:
The first part of the code is the Main
method, which is the standard program
http://www.dotnetperls.com/stelemhttp://www.dotnetperls.com/newarr8/12/2019 C# String Array
3/6
entry point.
Then:
A new
instance of
the Test class is created.
That class contains an array of strings.
Program that uses string array: C#
class Program{ static void Main() { // Create new instance with string array. Test test = new Test();
// Loop over elements with property.
foreach (string element in test.Elements) { System.Console.WriteLine(element); } System.Console.WriteLine(test[0]); // Get first string element }}
public class Test{ ///
/// String array field instance. /// string[]_elements = { "one", "two", "three" };
/// /// String array property getter. /// public string[] Elements { get { return _elements; } }
/// /// String array indexer. /// public string this[int index] { get { return _elements[index]; } }}
Output
8/12/2019 C# String Array
4/6
8/12/2019 C# String Array
5/6
Array Property, Return Empty Array
TypesThis article focuses on
string arrays, and string
arrays are different
from other arrays insome ways. Strings are
the only data type you
can declare with quoted
values. Also, strings are
reference types and can
be null, unlike integers.
Even thoughstrings
and string arrays are
different in their syntax,
the general usage
remains the same. You
could substitute int
arrays or object arrays and the balance of the
program would not change much.
Convert:A common problem with string arrays is
converting them to other types of arrays
and strings. Custom methods are often
used.
Tip:
This site has more detailed information
about converting string arrays to strings,
and char arrays and strings.
Convert String Array to String
LoopsTo continue, you can loop over string arrays in
your C# programs. There are two main ways
to loop over string arrays: the foreach and for-
loops. These loop constructs are covered in
http://www.dotnetperls.com/convert-string-array-stringhttp://www.dotnetperls.com/array-property8/12/2019 C# String Array
6/6
Programming in C#
C# Code
C# Tutorial
http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-4712093147740724&output=html&h=90&slotname=2814286994&adk=272650880&w=200&lmt=1378587238&flash=11.8.800&url=http%3A%2F%2Fwww.dotnetperls.com%2Fstring-array&dt=1379450355806&bpp=11&bdt=552&shv=r20130912&cbv=r20130906&saldr=sa&prev_slotnames=6682575170%2C4020807141%2C7895882516&correlator=1379450356439&frm=20&ga_vid=1253452052.1379450357&ga_sid=1379450357&ga_hid=864017601&ga_fc=0&u_tz=-300&u_his=6&u_java=1&u_h=768&u_w=1280&u_ah=728&u_aw=1280&u_cd=32&u_nplug=10&u_nmime=20&dff=verdana&dfs=18&adx=162&ady=4462&biw=1263&bih=600&oid=3&ref=https%3A%2F%2Fwww.google.com.jm%2F&vis=2&fu=0&ifi=4&dtd=6072&xpc=31cSfra7yW&p=http%3A//www.dotnetperls.com&rl_rc=true&adsense_enabled=true&ad_type=text&ui=rc:0&oe=utf8&height=90&width=200&format=fpkc_al_lp&fs=large&f=verdana&kw_type=radlink&prev_fmts=200x90_0ads_al&rt=ChBSOL36AAIY-go0LJQqAHHcEgtDIyBUdXRvcmlhbBoInKIt14KrEAcg37XPICgBSN-1zyBSEwiQ9r-GpdO5AhUiLjQKHXI1ADY&hl=en&kw0=Programming+in+C%23&kw1=C%23+Code&kw2=C%23+Tutorial&okw=C%23+Tutorialhttp://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-4712093147740724&output=html&h=90&slotname=2814286994&adk=272650880&w=200&lmt=1378587238&flash=11.8.800&url=http%3A%2F%2Fwww.dotnetperls.com%2Fstring-array&dt=1379450355806&bpp=11&bdt=552&shv=r20130912&cbv=r20130906&saldr=sa&prev_slotnames=6682575170%2C4020807141%2C7895882516&correlator=1379450356439&frm=20&ga_vid=1253452052.1379450357&ga_sid=1379450357&ga_hid=864017601&ga_fc=0&u_tz=-300&u_his=6&u_java=1&u_h=768&u_w=1280&u_ah=728&u_aw=1280&u_cd=32&u_nplug=10&u_nmime=20&dff=verdana&dfs=18&adx=162&ady=4462&biw=1263&bih=600&oid=3&ref=https%3A%2F%2Fwww.google.com.jm%2F&vis=2&fu=0&ifi=4&dtd=6072&xpc=31cSfra7yW&p=http%3A//www.dotnetperls.com&rl_rc=true&adsense_enabled=true&ad_type=text&ui=rc:0&oe=utf8&height=90&width=200&format=fpkc_al_lp&fs=large&f=verdana&kw_type=radlink&prev_fmts=200x90_0ads_al&rt=ChBSOL36AAIY9Qo0LJQqAHHcEgdDIyBDb2RlGgikRVuANpbpRCDftc8gKAFI37XPIFITCJD2v4al07kCFSIuNAodcjUANg&hl=en&kw0=Programming+in+C%23&kw1=C%23+Code&kw2=C%23+Tutorial&okw=C%23+Codehttp://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-4712093147740724&output=html&h=90&slotname=2814286994&adk=272650880&w=200&lmt=1378587238&flash=11.8.800&url=http%3A%2F%2Fwww.dotnetperls.com%2Fstring-array&dt=1379450355806&bpp=11&bdt=552&shv=r20130912&cbv=r20130906&saldr=sa&prev_slotnames=6682575170%2C4020807141%2C7895882516&correlator=1379450356439&frm=20&ga_vid=1253452052.1379450357&ga_sid=1379450357&ga_hid=864017601&ga_fc=0&u_tz=-300&u_his=6&u_java=1&u_h=768&u_w=1280&u_ah=728&u_aw=1280&u_cd=32&u_nplug=10&u_nmime=20&dff=verdana&dfs=18&adx=162&ady=4462&biw=1263&bih=600&oid=3&ref=https%3A%2F%2Fwww.google.com.jm%2F&vis=2&fu=0&ifi=4&dtd=6072&xpc=31cSfra7yW&p=http%3A//www.dotnetperls.com&rl_rc=true&adsense_enabled=true&ad_type=text&ui=rc:0&oe=utf8&height=90&width=200&format=fpkc_al_lp&fs=large&f=verdana&kw_type=radlink&prev_fmts=200x90_0ads_al&rt=ChBSOL36AAIY6go0LJQqAHHcEhFQcm9ncmFtbWluZyBpbiBDIxoICFLJ0w1zifgg37XPICgBSN-1zyBSEwiQ9r-GpdO5AhUiLjQKHXI1ADY&hl=en&kw0=Programming+in+C%23&kw1=C%23+Code&kw2=C%23+Tutorial&okw=Programming+in+C%23http://www.google.com/url?ct=abg&q=https://www.google.com/adsense/support/bin/request.py%3Fcontact%3Dabg_afc%26url%3Dhttp://www.dotnetperls.com/string-array%26gl%3DJM%26hl%3Den%26client%3Dca-pub-4712093147740724&usg=AFQjCNFx1N-NTsG-cB6cd-fw_pxEr3J2Uw