Jump to content

C# - What is an array?

kaddle

to be clear, i learned the basics of arrays including the basics on using arrays and i have a complete understanding of those basics. my confusion is regarding the classification of an array. when i see and use arrays they are clearly like a special type of variable since arrays can store multiple values. the book i'm learning from deliberately classifies arrays as a "advanced data type" along with strings and lists in the same classification (i'm assuming because arrays, strings, and lists have properties and methods). 

 

so essentially i'm wondering why arrays would be mentioned as being advanced data type along with strings and lists.

Link to comment
Share on other sites

Link to post
Share on other sites

A variable is simply an instance of a data type. Let me give an example.

String is a data type. So is integer. 
 

int x = 6

x is a variable, of type integer

 

String y = "abc"

y is a variable, of type string

 

int[] arr = {1, 2, 3}

arr is a variable, of type int array


So it is not "more special" than any other data type

Main Rig: R9 5950X @ PBO, RTX 3090, 64 GB DDR4 3666, InWin 101, Full Hardline Watercooling

Server: R7 1700X @ 4.0 GHz, GTX 1080 Ti, 32GB DDR4 3000, Cooler Master NR200P, Full Soft Watercooling

LAN Rig: R5 3600X @ PBO, RTX 2070, 32 GB DDR4 3200, Dan Case A4-SFV V4, 120mm AIO for the CPU

HTPC: i7-7700K @ 4.6 GHz, GTX 1050 Ti, 16 GB DDR4 3200, AliExpress K39, IS-47K Cooler

Router: R3 2200G @ stock, 4GB DDR4 2400, what are cases, stock cooler
 

I don't have a problem...

Link to comment
Share on other sites

Link to post
Share on other sites

array isnt special. You won't say a 14-alphabet long word is special to a 3-alphabet one right?

CPU: i7-2600K 4751MHz 1.44V (software) --> 1.47V at the back of the socket Motherboard: Asrock Z77 Extreme4 (BCLK: 103.3MHz) CPU Cooler: Noctua NH-D15 RAM: Adata XPG 2x8GB DDR3 (XMP: 2133MHz 10-11-11-30 CR2, custom: 2203MHz 10-11-10-26 CR1 tRFC:230 tREFI:14000) GPU: Asus GTX 1070 Dual (Super Jetstream vbios, +70(2025-2088MHz)/+400(8.8Gbps)) SSD: Samsung 840 Pro 256GB (main boot drive), Transcend SSD370 128GB PSU: Seasonic X-660 80+ Gold Case: Antec P110 Silent, 5 intakes 1 exhaust Monitor: AOC G2460PF 1080p 144Hz (150Hz max w/ DP, 121Hz max w/ HDMI) TN panel Keyboard: Logitech G610 Orion (Cherry MX Blue) with SteelSeries Apex M260 keycaps Mouse: BenQ Zowie FK1

 

Model: HP Omen 17 17-an110ca CPU: i7-8750H (0.125V core & cache, 50mV SA undervolt) GPU: GTX 1060 6GB Mobile (+80/+450, 1650MHz~1750MHz 0.78V~0.85V) RAM: 8+8GB DDR4-2400 18-17-17-39 2T Storage: HP EX920 1TB PCIe x4 M.2 SSD + Crucial MX500 1TB 2.5" SATA SSD, 128GB Toshiba PCIe x2 M.2 SSD (KBG30ZMV128G) gone cooking externally, 1TB Seagate 7200RPM 2.5" HDD (ST1000LM049-2GH172) left outside Monitor: 1080p 126Hz IPS G-sync

 

Desktop benching:

Cinebench R15 Single thread:168 Multi-thread: 833 

SuperPi (v1.5 from Techpowerup, PI value output) 16K: 0.100s 1M: 8.255s 32M: 7m 45.93s

Link to comment
Share on other sites

Link to post
Share on other sites

39 minutes ago, kaddle said:

"special data type" along with strings and lists in the same classification

I'd keep reading. I would bet that he's calling certain things "special types" because he's later going to explain classes and objects more thoroughly. If you're just now covering arrays, you're still at the beginning of the book (if not, get a better book).

Or, at the very least, is calling arrays "special" because it's "a variable that holds more than one thing".

 

What book are you reading?

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

lol whoops, my bad. i went back to read the quote and it is actually written as "advanced data type". not special. i think this type of description was mainly a way of describing that the data types have more features than others since they have properties and methods. but not that the data type itself is a different type of data type. i'll also edit the OP. my only confusion is why arrays would be under the classification of data type along with strings and lists.

 

the book itself is treating me very well so far and i'm learning a lot. i'm still early on in the book.

 

Edit: the book i'm reading is "learn C# in one day and learn it well". i suppose i'm going through the book a bit slowly since i'm documenting all of the information in a comprehensive way for me (not copy paste). i'm also practicing some of the things i learn in visual studio which takes more time.

Link to comment
Share on other sites

Link to post
Share on other sites

50 minutes ago, kaddle said:

my only confusion is why arrays would be under the classification of data type along with strings and lists. 

In C#, there are two classifications of types: Reference types and Value types.

  • Value types are passed around by creating a copy of the contents of the type and passing the copy to the function.
  • Reference types are passed around by giving the "address" of the object to the function.


The builtin Array type is a reference type.


While it is true that "String" and "Array" are both reference types. That's about where the similarities end. The problem with classifying builtin types is that the compiler/runtime often make what can only be described as "special dispensations" for the builtin types. For our purposes, "special dispensation" means that the type exhibits behavior that cannot be recreated without modifying the compiler/runtime. For example, String is treated as a reference type in some ways, and treated as a value type in other ways.

For trivia, the definition of the Array type is here: System.Array


Also, arrays could be considered "special" in the sense that it is baked into the language. For example:

// this is the normal declaration of a 
// variable of type "non static reference type".
// this is not, however, how we get arrays.
Array<int> myArr = new Array<int>(10);

// Instead, the compiler makes a 
// "special dispensation" for System.Array
// For example:
int[] myArr = new int[10];

 

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

On 2/25/2019 at 2:03 PM, straight_stewie said:

Actually, he's wrong there.

In C#, there are two classifications of types: Reference types and Value types.

  • Value types are passed around by creating a copy of the contents of the type and passing the copy to the function.
  • Reference types are passed around by giving the "address" of the object to the function.


The builtin Array type is a reference type.


While it is true that "String" and "Array" are both reference types. That's about where the similarities end. The problem with classifying builtin types is that the compiler/runtime often make what can only be described as "special dispensations" for the builtin types. For example, String is treated as a reference type in some ways, and treated as a value type in other ways.

For trivia, the definition of the Array type is here: System.Array


Also, arrays could be considered "special" in the sense that it is baked into the language. For example:


// this is the normal declaration of a 
// variable of type "non static class".
// this is not, however, how we get arrays.
Array<int> myArr = new Array<int>(10);

// Instead, the compiler makes a 
// "special dispensation" for System.Array
// For example:
int[] myArr = new int[10];


What book are you reading? If the author is really saying things like that, you should really consider getting a better book that contains less misinformation.

the book i'm reading is: learn c# in one day and learn it well.

 

i think what's happening in this thread is probably that i'm just bad at explaining what i'm talking about. sorry for that. it's written in the book that it will talk about value and reference types after explaining the basics about arrays, strings, and lists. i read the part about arrays already and i'm almost done reading the part about strings, so i did not reach the part where it explains about value and reference types. i can tell the book isn't perfect but so far it has been the most comprehensive learning material that i tried to learn c# from, so i'm not sure that the book is the problem. i suppose i'll have a better idea once i read more pages. the only bit of confusion i have is the way arrays were described in the book.

 

Spoiler

it says that an array is an advanced data type, but in the part about arrays it says the square commas indicate that the variable is an array instead of a normal variable. so i'm confused about how it can be a data type and also be a variable at the same time. so maybe i'm just confused because i'm still new to C#? it sort of makes sense in the context that a variable is an instance of a data type, as someone explained above. yet at the same time a variable itself is a storage container, while the data type is the type of data being stored.

 

Link to comment
Share on other sites

Link to post
Share on other sites

18 minutes ago, kaddle said:

so i'm confused about how it can be a data type and also be a variable at the same time.

I had the same problem when I was starting out as well. I found it hard to know what to ask, and on the rare occasion that I knew what to ask, I often didn't know how to ask it.

 

A "variable" is just a name that we can use to access "things".
A "type" is a way of describing what the "thing" actually is: What data it has and what actions it can do.

 

For example:

int a = 0;  // a is a variable of type int with value 0.

List<int> myList = new List<int>(); // myList is a variable of type List<int>

 

18 minutes ago, kaddle said:

square commas

Also

  • [ ] are square brackets
  • { } are curly braces
  • < > are angle brackets
    • Are also called less/greater than or open/close tag, depending on context.
    • <> are also known as guillemets, but that is not common knowledge.

 

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

Aaaaaand later you will figure out that string is actually a char array.

 

Long story short, it might be confusing at first but you will get it eventually.

 

The simple answer to what an array is would be "It is something that represent a collection of things"

Link to comment
Share on other sites

Link to post
Share on other sites

An array is “advanced” because it can store multiple values. 

 

int

byte

char 

all store a single value. 

 

However arrays, lists & strings are a collection of values that can be accessed by an index. 

 

The index indicates the place of the data you’re asking for. 

 

Strings are an array of characters. 

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, kaddle said:

so essentially i'm wondering why arrays would be mentioned as being advanced data type along with strings and lists.

It's "advanced" in that it represents something more than a primitive value. Basically, if the data type is not representing a single number or character, it's an "advanced" data type.

 

3 hours ago, kaddle said:

it says that an array is an advanced data type, but in the part about arrays it says the square commas indicate that the variable is an array instead of a normal variable. so i'm confused about how it can be a data type and also be a variable at the same time. so maybe i'm just confused because i'm still new to C#? it sort of makes sense in the context that a variable is an instance of a data type, as someone explained above. yet at the same time a variable itself is a storage container, while the data type is the type of data being stored.

I'm going to assume you're confused by something like this:

int[] foobar = new int[5]

foobar[0] = 12345;

someFunc(foobar)

The second statement is writing a value to some index in the array, which you have an idea of. The third statement however, is using foobar as-is without the square brackets which doesn't make sense when you were told this is an array and so you must use it like the second statement.

 

To explain this, there's an important distinction as to what a variable can hold. A variable can either hold an actual value, or it can hold a reference. A reference is basically a memory location and going to that memory location holds the actual value. While this sounds kind of wasteful (why spend two chunks of memory representing a single thing?), it's actually pretty powerful.

 

If you'd like to have a British professor explain the concept to you (note this is explaining things using C, but it's similar enough):

 

But otherwise, what foobar represents is not a value, but a memory address to a chunk of memory that contains the five values in the array, with foobar itself containing the the address where the first element of the array is. This is something that C# hides from you because if you don't manipulate references correctly, you can royally screw up the program.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×