leobarry
Badges: 10
Rep:
?
#1
Report Thread starter 1 year ago
#1
Hi
in C# are those variables
( private int, private float[], public bool, public void, public string)

new to coding.
thank you.
0
reply
winterscoming
Badges: 19
Rep:
?
#2
Report 1 year ago
#2
(Original post by leobarry)
Hi
in C# are those variables
( private int, private float[], public bool, public void, public string)

new to coding.
thank you.
No, those aren't variables in themselves since they don't have names. What you've got there is sort of incomplete - just a bunch of C# keywords which "could" be used when you make a variable, but they could also be used for something else.


A variable..
1) Is a name that your program uses to identify some small piece of the computer's memory...
2) Allows a program to work with that piece of memory: (storing things, or modifying whatever is stored)
3) Has a type which provides the C# compiler with enough information to let you work with that memory in a useful, meaningful way.

Variables might look like some of these these:
Code:
int myLittleNumber; // an int variable called myLittleNumber
myLittleNumber = 5; // modify the memory for myLittleNumber to change it to the value 5

string woolyJumper; // a string variable called woolyJumper
woolyJumper = "sheep"; // modify the memory for woolyJumper to change it to the value "sheep"

float boatyMcBoatFace;  // a float variable called boatyMcBoatFace
boatyMcBoatFace = 12.34;  // modify the memory for boatyMcBoatFace to change it to the value 12.34

---
int, float[], bool, and string are all types (data types / types of data); Meaning that they give the compiler information about the things/data being stored and used in memory. int is for integers/whole numbers, string is for plain text, bool is for true/false values, and there are loads of others.

void looks like a type, but it's not; it's special. It's the opposite of a type since it means "no type". I'll come back to that one, but just as maths has the concept of zero to mean 'nothing', there are some computer science concepts which can also benefit from having the same idea of 'nothing' or 'non existant'.

Types can be used in a variety of ways, but they all essentially boil down to C# providing a program with enriched, meaningful ways of using data. (Data is just a sequence of 1s and 0s like 1010111001101100110, which is not very helpful for a human brain. The computer doesn't care about types, but humans do, and C# is designed to be human-friendly rather than computer-friendly)

As above, C# can define a variable, whose type is int (a whole number), or a variable whose type is string (plain text), etc.

In addition to variables, you can create Methods, which are names that identify some block of code. Methods are sometimes known in other languages as "functions" or "subroutines" or "procedures".
- Variables always have a type.
- Methods sometimes have a type.
- If a Method does not have a type, it needs "no type" -- a.k.a. 'void'.

a C# program always has at least one Method, called Main(). -- notice the parenthesis () -- that's the important thing which tells you it's a method and not a variable. Notice that the Main() method in C# does not have a type, therefore it is a void Method.

If a method does have a type, it will be expected to provide some kind of data/value back to whichever part of the program actually uses that method. For example, when you use Console.ReadLine() in C#, that's a method which provides your program with a string from the user (so people might sometimes call that a 'string method').

---
There's one other thing to mention on the topic of 'private', but if you're new to programming, it's going to be "so what?, who cares?". Some things are worth learning and remembering as a beginner, yet private is not one of those things. The term for it is probably going to be even more confusing and nonsensical, but the official jargon for that is "access specifier". There are other access specifiers too, called public, protected and internal.

Access specifiers exist because humans are sometimes bad at writing code, and they're even worse at using other people's code. They exist because object-oriented programming is about helping programmers write code which is "easier" for other programmers to understand, and "harder" for them to make a blundering mistake. They're superfluous in some regards, They don't even exist in languages like Python or the "C" language. Just forget they exist in C# for now; they're kind of important but they're not that important.

You don't need to know or care about these until you are learning about Object-Oriented programming since they tie into ideas about design and programming habits. Visual studio will put them in your code for you because they're part of the language. Just try to pretend they don't exist for now. Leave them alone and focus on more interesting things instead.

If you happened to remove them from your code completely, then your program should still carry on working as it did before since they don't really 'do' anything interesting.
Last edited by winterscoming; 1 year ago
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Current uni students - are you thinking of dropping out of university?

Yes, I'm seriously considering dropping out (22)
17.32%
I'm not sure (3)
2.36%
No, I'm going to stick it out for now (41)
32.28%
I have already dropped out (3)
2.36%
I'm not a current university student (58)
45.67%

Watched Threads

View All