declare

9: How To Create Arrays In C# | C# Array Tutorial | C# Tutorial For Beginners | C Sharp Tutorial



Sharing buttons:

sometimes we write c-sharp they can be a

lot of data inside our code and in some

cases it might be easier to create an

array instead of creating a bunch of

different variables that are going to

have different data inside of them so

just to talk about variables we're going

to do that in this short episode here as

you can see in front of me I have a

different example than I did in the

previous episode in this video we just

simply have the main programs of CS file

and inside the main method we have four

different variables that are equal to a

string datatype now because all this

data here sort of belong together I can

put them all inside one array that

doesn't look as messy as what we have

right here sizing is instead of creating

all these different variables under each

other you can quickly see that if we

were to have let's say 50 variables it's

gonna get quite messy inside the code so

instead we can create what is called an

array now if you come from a different

programming background you might notice

that when we're about to create an array

that it looks a little bit different

than other programming languages such as

PHP or JavaScript because when it comes

to c-sharp when we create an array we

need to make sure you only use one

specific data type inside the arrays and

we also need to know beforehand how many

pieces of data that is going to go

inside our arrays so if I were to go

inside the code here and create an array

that is going to have the same data

inside of it as we have above here what

we need to do to start with is first of

all define what kind of data type we're

going to use inside this array because

remember we can only use one type of

data so in this case here we're working

with strings so I'm just simply gonna

create a string array so we're gonna say

we have a string and then we need to put

brackets right after to say this is a

array we're about to create here then we

need to define some kind of name for

this array we could call this one names

just to give it something and then what

we need to do is we need to actually

create memory or allocate memory for

this array just like we want to do if we

were to create an object inside c-sharp

and we do that in the same way as if we

were to create an object inside c-sharp

so we're going to use two new keyword

then we're going to say what kind of

data is this going to be this is going

to be a string array and

then we're going to determine inside a

pair of brackets what exactly the number

of data is going to be inside this array

if I did call this one curly brackets

before I do a poly test that is called a

bracket just to sort of fix myself if I

called it that so inside the brackets

here right now I can actually tell we

have four pieces of data that I need to

insert inside this array so I'm going to

write four inside the brackets here now

we can if we already know what is going

to go inside the array to begin with we

can actually assign data directly on

this one line here so afterwards what I

can do is I can actually go and use

curly brackets and this time it it

access a correctly and inside the curly

brackets say what we can do is we can

actually go ahead and create the

different strings that are going to be

inserted inside the array so I can say

the first one's going to be Daniel then

we're going to separate them with a

comma the next one is going to be John

and then we're going to separate them

with a comma the next one is going to be

Jane then the last one is going to be

Toby there we go so this is how it would

actually create an array with these data

inside of it now if I don't know how

what kind of data I'm going to put

inside the array I can just go ahead and

delete what I have here and see how

we're gonna insert the data afterwards

so what we can do is we can actually

assign data later on our code if we

don't know what it's going to be

inserted as data into the array when we

do actually create it and the way we do

that is by referring to the array which

is in this case is called names and then

we need to refer to the index or the

plays inside the array because you need

to think about the array as a container

that has spaces that you need to sort of

fill in inside the array and because we

have four spaces inside this array here

because we created four different

indexes we call it we can actually go

ahead and assign each piece of data to

one of these indexes now indexes inside

the array to simply mean what place

inside the array we're going to fill in

and when it comes to indexes or places

inside arrays we always start counting

from zero and that is something that is

very important to note anytime we do

anything with code we usually start at

zero

so the first spot inside the array is

not going to be spot number one it is

going to be spot number zero so what I'm

going to do is I'm going to say we have

names and the 0th index inside the names

array and I want to set that equal to a

string data which is going to be Daniel

in this case here then afterwards we can

go and fill in names one and then names

two or names three not names four

because zero counts as one of the places

if you want to duplicate a line just

simply right ctrl D ctrl D if that was

hard for my from my accent to understand

and then you can just sort of copy the

line here and then create a new index

there we go and then fill in different

data then in this example here who were

to fill them all in like so and actually

you run let's say name number one well

in this case not name number one but in

this case we might run name number zero

index there we go and then run the

console then you can see if we were to

pull it over there we get Daniel inside

the console is what to refer to names

with index as two then we should be

getting Jane inside the console and

again I have to pull it over I don't

know why I need to do that and there we

get Jane now creating a race like this

is a great way to do it if you want to

sort of collect data in one place

instead of having them all spread out or

have them separated in different

variables in your code so a race is

something that we will be using quite

often inside our applications now for

now we're not going to focus too much on

the fact that we have arrays and you

know instead of variables because you're

still new at learning c-sharp and if you

haven't learned something like I don't

know PHP or JavaScript before then

creating a race might be a new concept

for you which in that case you shouldn't

be too worried about a race because we

will get familiar with them as you start

coding inside c-sharp here so it will

sink in at some point so don't worry too

much about it but that is what I wanted

to teach a day when it comes to a race I

hope you enjoyed and I'll see you in the

next video

[Music]

[Music]