โ what's an object?
Can somebody please explain to me in very simple words what's an obkect as I don't understand it from explanations on the internet
740 Replies
It's a thing
A car is an object
A desk is an object
The chair you're sitting on is an object
It's not any different when it comes to programming
So it's just a variable?
No
Like x=10?
A variable is the object's address, you could say
What address
How you refer to that specific object
Like, you have a
Car
. But which car do you mean?I still don't know what objects are
So Im not sure I understand
Two different objects, two different cars
So each has a different name
There's my car, and there's your car
And they're both new cars?
They're both cars
New cars
new
is how you instantiate an object in C#What
How you create a new instance of a car
A new instance of an object
Idk even know what instantiate means๐ญ
To create an instance of
An individual thing
A class, in C#, is sort of a blueprint for a thing. A concept of it, an idea of it
Then, from that class, you can create a specific instance
Why make new instance of a car?
A specific, existing, physical object
Because you want an actual car
Not just an idea of a car
You already have it just copy it
What if both have different colors?
But you didn't write that anywhere
Im confused
And even in the case of
what if I want to start just a specific car?
I don't want to start the idea of a car
I want to start specifically my own car
What idea?
Are two black fords the same car? no, they are two different instances of a black ford
The idea of a thing, the concept of it, not the specific given thing
What?
"A chair" is a general term, a name for the concept of this piece of furniture
"A chair" doesn't have colour or shape
"The chair" does
When I say "chair" it doesn't have colour
When I say "my chair" or "your chair" or "that chair over there", they do
They're no longer abstract concepts, they're concrete, existing, physical objects
But only if you put that color in parentheses
That's a constructor
You can just... not have a constructor, and it can still work
Sorry I have no idea about the wording and in general Im just lost
Here, I just set the colour of those cars after creating them
You just called myCar a variable
Then isnt it a variable?
The variable gives that object a name
So objects are variables
No
Are you your name?
Or are you a person, that has a name we can all use to refer to you?
Two different people, with two different names
What pobiega?
@Pobiega
Another user
๐
I dont understand
You don't understand a difference between a person and their name?
We use names if we want to refer to specific people
But what about my name?
What about it?
It's how others can tell you apart from other people?
It's how others can refer to you?
So its indetificator for an object?
Yep
in programming we say that "a variable points to an object"
And its a variable
no
its two different things. One points to the other
They just used var
a name points to a person
var variableName = new Person();
var variableName
is indeed a variable
but new Person()
is notYeah so my identuficator is a variable
yes
So my name is var
no,
var
is a keyword
variableName
is the nameI meant short for a varaible
okay, then yes
My name is a variable string
Or something like that
Soinagie
is a "variable" that points to an object discord userAnd its identificator for object person
But I still don't really know what objects are
If its not a value or a method then what is it?
its not a variable, its not a method. its an object
its its own term
an object is an instance of a type
Type of what?
a type.
string
is a type for exampleNot sure how deep youre into math
But in maths you also call letters that act as placeholder for a value" variables"
Like x*2, where x is a placeholder for whatever value you want it to be
x*2 turns into 2*2 when x = 2
Now you can also do the same for primitives (numbers like int, float, etc.)
Because the values for primitives are so simple, you can write them as constants / literals in your code just like that (
MultiplyTimesTwo(1)
)
But if you have complex data types like a whole Person
, like so
You cant just hardcode a literal value of that into code when using it
This class acts as a group of variables. One instance of Person
has an Age
, a FirstName
, a LastName
inside of it.
When you want to create a new group for, let's say, me, you'd create 1 instance - 1 object - of this class via
That specific variable wholesomePerson
now contains an object of type Person
int
, ยดCar` etcIts a variable
what is?
a type? no
String
no, thats type
And objects are types of what then?
thats a variable called "myString" and the type is
string
objects are instances of typesWhat does that mean?
thats what we just discussed with the cars and chairs
objects are not types
.
here, we have 3 variables, but only 2 objects
thirdCar and myCar both point to the same car
I very much appriciate your help but I doubt I'll understand any of this
objects are instances of types
Why is thirdCar treated like an object?
what do you mean by that
its not?
Its just a copy of myCar
no, its not a copy
it is not a copy
Its the same
its a new variable, pointing to the same car
pointing = meaning
third car is another name for my car
There is only newCar and all of these variables refer to it
A person can have multiple names
Here, I'm ZZZZZZZZZZZZZZZZZZZ, but my friends refer to me by my actual name
And in some games I use other names entirely
What does pointing= meaning mean?
new <space> Car
creates a new car
okay, lets slow down a bit
both variables mean the same thing
what do you think this snippet of code will print?
There are no "meaning" and "pointing" variables
It'll print same car blue?
Look at the last line of code
Console.WriteLine(myCar.Color);
Yeah myCar = sameCar
So it'll print sameCar
With color blue
you're right, but for the wrong reason :p
it will print color blue
because myCar is indeed the same car as sameCar
but it won't "print sameCar"
it will only print the color
Pobiega#2671
REPL Result: Success
Console Output
Compile: 732.596ms | Execution: 47.060ms | React with โ to remove this embed.
Why did you put black there?
If it isnt used
to have an initial value
Why
I wanted to demonstrate that they are the same object
You give it a value before you print it
yes
And Car and myCar are different things?
yes,
Car
is a type, and myCar
is a variable
what about this code then:
I thought Car was an object
no,
Car
is a type.I wanted to know about objects
objects are instances of types
you create an instance by using the
new
keywordAnd I still don't know what that means
so
new Car()
creates an objectobject is an instance of a class
But newCar is a variable
yes
class is a specific blueprint for the objects layout
So it isnt an object
correct. its a variable pointing to an object.
so when I say
var sameCar = newCar;
Im just saying "create a new variable called sameCar that points to the same object as the variable newCar does"It'll print black I guess
correct.
Could you please not use newCar as there already is word "new" being used and it just confuses me
you're the one who used it
I didn't.
^
you created it, I just went with it ๐
Bc I thought newCar and new Car are thevsame thing
no ofc not
especially not since
new Car
isnt valid
new Car()
is
the parenthesis is not optionalOk can we take it really slow please?
sure
What is a type?
You know how computers only actually work with 1s and 0s?
Yeah
well, thats annoying to work with for human programmers
but the computer still needs to know how much space of memory (how many 1s and 0s) to reserve for an object
What object?
an object
no actual object yet, a theoretical one
And what is it?
are you guys trolling
Who?
an object is an instance of a class
And I dont know what instance of a class is
We went through it at the start
You need some patience in this case, we're starting from ones and zeroes lol
types are... well, defined types
that the computer knows about, because we told it what they are
string
, int
, or Car
in our caseCar isnt the same as string and int
you can make your own, using classes (and other ways, but primarily classes atm)
its not the same, but its the same "category" of thing
Sorry idk what get set are
ignore it
I just wanted to learn from the top so I thought I should know what objects are first
this is a very small type declaration that creates a type called
Car
that has a property called Color
(and type string)
sure, but we can't explain objects without introducing other things
it doesnt make sense as a stand-alone definition
C# is a typed language
that means everything has a typeSo methods have types?
sure
public string GetName() { ... }
for example
thats a method that returns a stringWait a sec
Before we speed off
no, types have methods but methods can have return types
Is getName a method?
Akseli, you're really not helping at this point
So if anyone is trolling, its you.
yes
Ok so we made a string method
a method that returns a string, yep
Wait
when we call that method, we know we get something that points to a string back. it can be a new string, or a previously existing string
It doesnt say anywhere that it returns
sure it does.
it MUST
Or is it implied
im just making sure no misinformation is fed
Since its not a void
it won't compile if it doesnt return a string
the word string, in that position, is the return type
because we set its return type to string
I thought that you had to use "return" keyword
For every non-void
you do
And you didn't
you see how I left out the body of the method with
...
as the placeholder?thats in the implementation (code block which is hidden)
Im not sure if hiding things for begginers is a good solution but sure
for brevity
well I wanted you to focus on the important part
the type in the method signature
What signature?
public string GetName()
thats a "method signature"Its a method
it declares the method
it is, but its specifically the signature of the method
when you say "method", you often mean the entire thing, including the body
Ok so its creation of a method
yeah
and its type is a creation of a method type?
?
Or do you mean string
its return type is string
So this block is creation of a method that'll return a string
yep
and thats important, because we can now know that it wont return an
int
or a Car
it always returns a string.And thats important because?
imagine you have a method like...
public Car GetCar()
you know it returns a car, so you write code that uses the returned car
then suddenly you decide that GetCar
should return int
in C#, this will be an error
and your code wont compileI mean you can assign numbers to cars
in a language like javascript, this will be fine, but when you run the program, it will crash
sure but thats not what Im saying here, I mean actually returning a number instead of a car
But Im getting off track
Yes, u can assign numbers to cars, but a number is not a car.
so you can have
myCar.CarNumber = 69;
It expects a carthe returned car has a colour
when you have code that expects a car and uses the colour of that car, if you give it an int, it will not work because ints do not have a colour
Ok and why did you talk about Car and int not being compatible?
because you asked why its important that methods have return types
I thought that they have to have them
they do
Ok so objects are made up types
no.
its the chair thing again
Chair
is a type.
an object is YOUR chair, or MY chair
they are both chairs, but they are different, likely in both color, size and shape
but they are both chairsBut there is no myInt or yourint
sure there is
Pobiega#2671
REPL Result: Success
Compile: 347.904ms | Execution: 58.782ms | React with โ to remove this embed.
I just made one
And its an object?
myInt
is a variable
myChair
was also a variable, technically.It looks like a variable
a variable POINTING TO AN OBJECT
And object being...
Where does it point?
to somewhere in memory
What does even that mean?
new Car()
returns a pointer to memory. you store that in a variable and use itSo objects are just things you can point towards?
so the thing you remember need to be in memory, with an address (where it's located) but idk if you are going that far
reading through this thread here it sounds like trolling but way to dedicated
Wow thanks
So motivating
Car myCar = new Car()
new Car()
creates a new object of the type Car
Car myCar =
assign the variable "myCar" which is a reference to point to the new object createdIsnt myCar now the same as new Car?
no
new Car()
isnt a variable
new
is a reserved keyword used to create objectsBut you didnt even name it
This object
Its just a Car
imagine you have a table with 5 chairs, you know what a chair is, chair is a type, each one of the 5 chairs are objects of the type chair
objects dont NEED to be in variables to exist, just fyi
they dont HAVE to have names.
they very often do, but its not a hard requirement
I thought they were instances
for example objects in a list can have no name but exist
they are
an instance is not the same thing as a variable
If they dont have names and variables whats the point of them?
All of them are the same
Then
no
they are not
they might have different properties
you can have 5 different chairs with different properties like color and size but they are still all chairs
they follow the same blueprint
Yeah and they're all different so they should have different names
"names" isnt a thing
So you could separete them
do you name your chairs?
when you give a chair name in real life, the object (the chair) doesnt have a name, you just create a name that means that specific chair
i certainly dont name my chairs ๐
just like variables
I mean how will they know which chair am I talking about
you point to the chair
I give them names so other person know which one to pick up
"the second chair from the left"
And objects are just "second from the left"?
which can be "the second item in the list of chairs"
kinda
var secondChairFromTheLeft = new Chair();
SureYeah you just named it
lets say you have a dog, called jerry. when you talk about jerry to someone, you say jerry (the name), you dont take the dog to your hands and shove it in their face, you just reference to the dog (object) with a name
So I could use it now
This specific object
you could use it before too
this is fine
Before what?
before "giving it a name"
Sorry I dont know anything about lists yet
Look you're probably overthinking this by a lot
C# is object oriented language and I dont know what these objects are
you kinda do
you understood just fine when I did the
myCar = sameCar
thing
you understood that changing one changed the other, as they were the same object
but when I made them different cars, you understood that modifying one doesnt modify the otherhes trolling
So for example car.(color) is an object?
Or is it a type
Or something else
int car.(color)
is not valid code
so I cant tellOops
Remove int
there is no syntax to "write an object"
new T
will create a new object of type T
thats it
thats the closest thing we get
this is valid code, for example
but its kinda pointless, as you can't refer to the object any more and it will be cleaned upWhy would I make new objects?
you almost always "capture" the object and store its reference in a variable, so you can do things with it
because C# is object oriented
to structure data
its how we write code
if your application is a car inventory program
isnt it useful to keep track of cars?
And I still dont know what that means
@Soinagie Almost everything is an object in OOP programming
you keep going around in circles
Do you know why literally everything has ".ToString()" method ?
Wdym?
hes literally trolling, if hes at this level of basic like literally grandpa level he shouldnt know about copy semantics but he did and thats how he exposed that hes trolling
I'm starting to agree with you
If you're not willing to help you don't have to
but whatever im kinda bored
There is an "Object" class @Soinagie
Every classes inherits that Object class
Object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or properties), and the code is in the form of procedures (often known as methods).
A common feature of objects is that procedures (or methods) are attached ...
and the object class has a (virtual?) to string method
which means all objects will have it
So all objects can be strings?
yes
no
mb then
all objects can be "turned into" strings
all objects are not strings
but all strings are objects
not the other way around
I thought strings were a type
a type.
string
is a type.classes are types
there are primitive types and non-pritimitive types
So color is a non-primitive type
string
is a type, but an instance of a string is an objectBuilt-in types - C# reference
Learn C# built-in value and reference types
So "hello world" is an object?
yes
It's a string yes
In the object-oriented programming paradigm, object can be a combination of variables, functions, and data structures; in particular in class-based variations of the paradigm it refers to a particular instance of a class.
In a computer system, any time a new context is created based on some model, it is said that the model has been instantiated. In practice, this instance usually has a data structure in common with other instances, but the values stored in the instances are separate. Changing the values in one instance will then not interfere with the values of some other instance.
In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods)
system.drawing.color is (i think)
Usually you define an object with "new" keyword
Pobiega#2671
REPL Result: Success
Console Output
Compile: 495.486ms | Execution: 64.641ms | React with โ to remove this embed.
But C# has a feature called implicit and explicit operator
compiler uses that to create a string
So if I do int x=100 then x is an object?
stop introducing more and more unrelated things, the valid explanation for objects is here
^
as ive told you, there is no easy way to write a line of code and point to a thing in the code and say "see, here is the object"
it doesnt exist in code, it exists at runtime, in the computers memory
we use variables to REFER to objects
but that doesnt mean the variable IS the object
Simple question do I need to know what objects are?
yes definitely
not to the level of understanding you seem to seek
Then if I cant use objects why do I need to know about them
who said you cant use them
.
var x = new Car();
is using an object
its just that x
isnt the object
its a variable pointing to the objectBut this object was never created
[declaretation] = [assignment]
[type] [name] = [value]
I cant refer to something that doesnt exist
new Car()
creates itIt creates it
^^
So now x is an instance of car?
no, its a variable pointing to an instance of
Car
yes exactly
x
is never anything but a variableAnd whats that instance?
Of car
Ok you are trolling
a lot of ones and zeros
pick up two pens. they are two different instances of "Pen"
Yes they are
So theyre 2 objects
sure
Then where did you use an object?
If x isnt an object
creating it is using it
And car is class
x.Color
is using an object
etcclass is the class in classify or classification
just because
x
itself isnt the object doesnt mean you cant interact with the object with the variableAnd car is this object?
And its name is x?
go away
troll
Or is it its value
Tbh I dont know the differnce
x just refers to an object
real object is new Car()
So its it name
[declaretation] = [assignment]
[type] [name] = [value]
And you can refer to that specific object
yes
(simplification)
imagine doing this to an actual teacher in school holy shit
i wish i back in primary school
I wish i was curious like him when i start coding
Wdym by that?
except i was 10 but still
value is right side of =
declaration part is left side of =
string text = "Hello" for example
it doesn't need to be a class
Both x=10 and 10=x mean the same
no
= is used for assignment in programming languages (most of them)
not for comparison
Yeah I assign value 10 to x
= means literally get the value (right side of ๐
and put it in left side of =
Samarichitane#3472
REPL Result: Success
Console Output
Compile: 522.035ms | Execution: 27.166ms | React with โ to remove this embed.
So x isnt 10 but 10 is x?
what is 10 ?
Int
but "10" doesn't refer to a variable
left side is always the target
Compiler doesn't know is it variable or not
It's just an int like you said
not reference
or an object
a human's name can be
Soinagie
, but a Soinagie
's Name can't be a humanSo I assign value of right thing to the left thing
yes
And then x is a object new Car
yes
(no)
if
x
was the object, then how on earth would var y = x;
be valid?And new always means I create an object
there is still only 1 object
miles and tails refer to the same fox
Then whats the point of keyword "new"?
Yeah and you can use both interchangibly
but they are not the object
they are references to the same object
they are not "the name of the object"
in a sonic game, when you start a new level, they do
new Sonic();
and stuffReference is a name
You refer to me by using my name
so say reference
dont say name
We access references with name
Then this specific new human named soinagie jumps
yes
And soinagie is an instance of class human
yes
Or type
Class or type?
a class is a type
They're the same or is it like square and rectangle?
all classes are types, not all types are classes
yes, a class is one of the subcategories of a type
So a type is a kind of value (like color) and class is a template for objects with the same type
not sure what you mean
a class is a reference type
Reference to what?
in here for example, when you call Jump on soinagie, it knows that its refering to the human you just created
both miles and tails refer to the same fox
Yeah
so the same fox will jump twice
I get it
But class is a reference to what
its not a reference, its a "reference type"
So its a kind of specific type of value?
Like color?
its a type of reference
type of value is something else
Then is it reference type or type of refernce?
thats the same thing
And what is it?
Its a template for creating objects
yeah, u can have a single object, and multiple variables refer to the same object,
like tails and fox
value types cant
Isnt refering and containg value essentialy the same?
You use that value in both cases
idk i dont really focus on these terms
You just gave me a link to refernce types which separetes these two
integer is a value type
Tails is a refernce type then?
Fox is
And tails is...
a variable
What
that refers to the object fox you just created
I refers to fox
Both refer to fox
they both refer to a fox, the same fox. they are not references to the class itself
Then tails isnt a variable, its a reference
this is still going? cmon guys
tails IS a variable
Why?
tails is a reference
.
.
or a reference variable, i guess u can call it that
its both a variable and a reference
reference type and a reference are 2 seperate things
And same goes for miles
yes,
reference
, and reference type
are 2 different thingsWhat๐ญ
Type = A Car In General
Object = A Specific Car
Variable = That Car Over There
Isnt that car over there also a specific car?
I dont think so
Its not about the car, its about the pointing to a car
a variable is the thing that points to it
Yeah you point to a specific car
yes, think of life as a program, it created 100 million cars, and you have a car, you want to know which car to use
In both cases
Ok no the object is what you are pointing to and you are the variable
you are not a car
How am I relevant?
If you are pointing to a car you are a sort of variable
one person go at a time and try to explain it, so there wont be any conflicts introduced to his mind
(its more complicated than that but this is the simple version)
fair i feel like all of us mixing is making it more confusing haha
we've been trying to explain that for the last 2 hours
and it's very entertaining (no offense with that, I like it that you guys try your best)
he is trolling
but i enjoy watching it
They*
And Im not
I just want to understand that
Im not sure but i dont want to be rude to them if im wrong
Idk actually, I've seen other people that don't understand it for weeks until a bit of practical experience
Ill just leave this convo and read back what came out of it haha
scroll up and you will see
Ok @implicit electron can you explain to me varaiables and objects then?
If you wanna understand it go and write some programs, after a few days you'll get a better understanding
?
ur an object of human, Soinagie is ur variable, when someone calls ur name, we know we are refering to you
So variable is a specific object
Or a reference to specific object
So in case int x =10 x is both a variable and object
I can't wait for the confusion when they figure out that an object's type is object
careful, int isn't a reference type
10 isn't an object
X is
you need to explain the difference between reference and value types
Ok @implicit electron only talk for now please
no, x doesnt refer to 10, x itself is 10
when you talk about int, u are talking about a value type, not a reference type
I dont know the difference
take baby steps, fully understand one of them first
I mean you can write 10 a bunch of times
So they're new objects
And they're all the same variable?
Then which one Im supposed to learn first?
well you have some idea about reference types right? might need to continue to finish learning that first
I dont think I have
i gave you an example of miles and tails, what did you not understand from that
Then whats the difference between variable and refernce type?
Human is a reference type
I thought human was a class
well, class is a reference type
same meaning
we did mention that above, i think
And what specifically is a refence type?
didnt we just explain that?
.
thats a different issue
explaining that is way easier than the concept of reference types though
Int is a type
yes
Int is a type, but not a reference type
its a Value type
And reference types are made up types
Like color
No.
Color
could very well be either a value type or a reference type, we cant tell without being more specific about what color you meanWhy is programming so hard to learn
depends on the person
Itโs not
It's all learning by doing imho
I mean car(color)
I would highly recommend you get started programming, instead of wondering on the theoretical definition of every single term
There is no default color type in C#
You create it
So its made up
but what if I "made" it like...
just because I made it doesnt mean its a reference type
you can make value types too, if you want
And its a variable or refernce type?
The code you just made
thats an enum, its neither
its a value type
@Akseli please don't assume people are trolling, it's just rude and disrespectful in the case that the person is genuinely sincere
Then how is it relevant?
its not an assumption
because you just claimed all "made up types" are reference types
which they are not
yes it is, where did they state they are trolling
Soinagie you wonโt get anywhere if you get stuck trying to find the definition of every little thing, just find something simple to make, and start making it. Youโll learn what various terms mean as you go along and apply them to your own applications
TheRanger#3357
REPL Result: Success
Console Output
Compile: 620.882ms | Execution: 33.214ms | React with โ to remove this embed.
read the convo from start...
700 messages?
TheRanger#3357
REPL Result: Success
Console Output
Compile: 569.295ms | Execution: 43.221ms | React with โ to remove this embed.
you only need to read the first 100 to see it
@Soinagie tell me the difference between the 2 codes i posted above
if you don't want to help then just... leave this thread
Theres a class and struct
But I dont know how they're different
what about the Console output?
why did one print 0 , and the other printed 10?
Bc fox wasnt a class?
In 1st one
yeah, but why
I dont know
simple
this
I personally can't find anything that suggests anything other than that this person is just trying to understand.
cloned tails
so now you have 2 foxes
Cloned?
like, copied
Where
here
Miles and tails arent foxes
just try to help him, im not bothered anymore
they are
They're a value for a fox
Or a refernce
Or something else
I dont know
since fox is a struct in the first code, the variable isnt a reference anymore
I dont know what a struct is really
its a value type, like Int
Int is a struct
when you do
var miles = tails
, it copies tails and assigns it to miles
because Fox is a struct, not a classAnd why does it work with class fox?
What does that class do?
because class is a reference type, struct is a value type
many variables can refer to the same object, struct cant
Why does it matter if it was copied?
depends on the programmer
If it cant reference it
Since its not a class
do you want it to be copied or not
No but it still doesmr matter
It cant even get access to it
if int is a class
You just said that int isnt a class
and you have many 1's in your program, what if you changed 1 to 2
all of the 1's in ur program will become 2
if
If what?
sounds like ur not good at english, no offense
so i believe its hard to communicate with you
Ok but what did you mean with that?
TheRanger#3357
if int is a class
Quoted by
<@!105026391237480448> from #what's an object? (click here)
React with โ to remove this embed.
note how he said if
i was saying
what if int is a class
say you have a company, with 20 employes that have 5000 salaries
if you tried to change 5000 to 10000
all of the 20 employess will have 10000 salaries
i hope my example isnt bad lol
brb 5 minutesThere are no refernces here
thats what the "if" was for
if int was a reference type, it would be impractical
Where is the "if" in this example?
What even does "if" has to do with any of it?
if int was a reference type, changing 5000 in one place would change it in all the places
Isnt it salary=5000
if as in the english word if, not the keyword.
And you change it to salary =10000
Then is it keyword or english word?
i was speaking about the contents of the number itself, not letting the variable point to another number
Number is the content
10 is 10
Theres nothing more to it
How does variable "point to"?
with value types, they dont
with reference types, thats how programming with reference types work
say for example you have defined a struct color
So strings and classes are reference types, rest of them are value types?
TheRanger#3357
REPL Result: Success
Console Output
Compile: 648.509ms | Execution: 37.902ms | React with โ to remove this embed.
tell me why that printed 255
TheRanger#3357
REPL Result: Success
Console Output
Compile: 590.516ms | Execution: 35.456ms | React with โ to remove this embed.
You asked me similiar question while ago and I still don't know
and why that printed 64
because one is struct(a value type) and other is class(a reference type)
and I dont know the difference
that is the difference
in the first one, color and another color's red is 255
in the 2nd one, color and another color points to a color that has 255 red
but another color what defined as 255
wdym
anotherColor = color
in which code?
color.red= 0
in 1st
whjat about it?
so it should print 0
why should it, i set color's red to be 0, not anotherColor's red
yeah and anotherColor = color
so they have the same value
it got copied
thats how value types work
I still dont know what you mean by that
when u do
anotherColor = color
its like u did anotherColor = new Color { red = 255 };
yeah
they both are 255
yeah, however in the first one, i only changed color's red to be 0, anotherColor's red stayed the same
but they're now the same thing now
essentialy
they have the same values
at first yes, but changed one of their reds
but you did the same with class color and it came out different
it shouldn't have
exactly
why not?
bc you set anotherColor as 255
because they are the same object
and in struct they're not?
no
it got copied like i said
copying doesnt erase a value
it didnt erase it
where did it erase it?
in color.red = 0 but it still printed 255
i printed anotherColor's red not color's red
and same goes for 2nd code
you did the same thing
yes,
because anotherColor and color refer to the same object in the 2nd code
then why use struct class?
whats a
struct class
public struct Color
to copy data without affecting the original like u see above
I still don't understand this copying thing
but now you see the difference, right?
i really have to go, cya later
well u can try messing around in ur visual studio with that code
maybe, kinda?
$refvsvalue
but you defied anotherColor as color so it should be 0 even without it, There's nowhere stated that anotherColor=255
you did anotherColor= color in both but it only worked in 1 and it doesn't make any sense to me
let me tell you how it actually works
in the 2nd code
new Color { red = 255 };
creates a value of that color and stores it somewhere in the memory
and variable color
refers to that value that is stored in the memory
so when you do anotherColor = color
now anotherColor
points to the same value in the same memory location as color
but in the first code
var color = new Color { red = 255};
stores the value in variable color
itself
it is not stored somewhere else in the memory, it is stored in variable color
itself
so when you do anotherColor = color
in the first code
it copies the value that is stored in variable color
, and takes it
again
for struct, var color = new Color { red = 255};
stores the value in variable color itself
for class var color = new Color { red = 255};
stores the reference of the value
for struct, var anotherColor = color
copies the value that is stored in variable color
itself
for class, var anotherColor = color
copies the reference that refers to the value that you initialized with new Color { red = 255};
I think thinking of this in terms of memory can be easier to understand. A variable refers to a named slot in memory. You can make new named slots by declaring variables. You can load data into that named slot like this
variableName = data
.
Variables have types, which define the size of the memory slot they identify and what data can go into that slot. Objects are just multiple named memory slots grouped together into a single one.
The type of a memory slot can be a reference (pointer), which just means it has the address in memory of some other memory slotif it copies the value shouldn't it then be 255?
for 1st one
who should be 255?
bc it copied the value
like you said
color or anotherColor?
anotherColor got value from color which was 255
yeah, it did
then if its got that value it should print it
it did
crap
I meant 0
Sorry
You first set color to 255
but then change it to 0
its lower in the code
so this one will be executed
TheRanger#3357
REPL Result: Success
Console Output
Compile: 682.319ms | Execution: 39.248ms | React with โ to remove this embed.
but you set anotherColor = color so they're the same
they have the same value
nope, it got copied
theyre not the same
= means equals
yeah
ur point?
that equals means these things are the same
sorry if Im annoying you
2 different objects that have the same red 255
x = 5, that means x is equal 5 and nothing else
ok in math
u have x = 5
and y = x
now y and x are 5 right?
it cant equal x = 6
right
what if lets say
5 = 20
is a thingit cant be
how?
= for assignment
== for comparison
we talked about this
:/
they arent asking for comparison atm
you cant assign a number other number
mathematically u cant yeah
i was giving an example
ok so
do u know pie
3.14?
yeah
x = pie
and y = pie
correct?
yes
what if i changed pie to 9
instead of 3.14
its a constant
i know but lets say we can change it
ok?
programatically
what would the value of x and y be?
9
if pie is a class
then yes, if its a struct, then no
here is a better example
is it all in a value or reference?
or outside?
like you do pie=3.14 then struct pie=x
or all inside struct?
TheRanger#3357
REPL Result: Success
Console Output
Compile: 590.352ms | Execution: 33.431ms | React with โ to remove this embed.
TheRanger#3357
REPL Result: Success
Console Output
Compile: 651.824ms | Execution: 34.013ms | React with โ to remove this embed.
why did you do new Pie()?
to initialize it
cant you do just Pie?
no
so I call all my methods with new?
?
Pie is a method right?
pie is not a method
Your are missing the main point @Soinagie
which Pie, the one next to new or next to class/struct?
He changed x's Value
you're referring to the constructor probably
but Y's value is changed too
Why ?
Focus here
bc x and y in reference type are treated as the same things?
yes
well thats I understand
but it shouldn't apply a completly different logic to a different type
what?
I mean value type
I dont understand value type
not sure what u mean
It gets the all properties
yet values are different
values not bound to anything
but theres "="
can we do another mathematical allegory?
for value type?
not sure what that means
we did pie for refernce type
yes
what I mean is could you elaborate why "no"?
that was an answer to ur question
if pie is 9 and pie=x=y then both are 9s
pie=x=y ?
what?
.
if its a class or a struct?
we did reference
yeah
class
and I would pls like to do struct
what about
x = pie
?what about it?
u pinged me for this
yeah you explained reference type
right?
yes
but for value type you just said "it doesnt happen"
where did i say that
what exactly
doesn't happen
?.
its not a 9 if its a struc
heres a better example
TheRanger#3357
REPL Result: Success
Console Output
Compile: 620.839ms | Execution: 32.997ms | React with โ to remove this embed.
what did u notice in this?
that y = x but they both show different values
yeah
and thats my problem
it copied it
if it copied it then shouldn't it be the same?
no, a copy is a copy
like u copy a file
yeah
its the same file
with same value
no, 2 different files, have equal data
I means their contents are the same
yes
if you modified one of the contents of the first file
would it also get modified in the 2nd file?
no
exactly
thats how struct works
wait so it creates a copy and then modifies it?
yes
and original value of 3.14 is never changed
the one that is stored in y yes
then what does var y = x do?
i told u, it copies it
u have a file called x
with contents inside
u copy pasted the file, and named it
y
y is the original and x is the copy of it
other way
x is the original, y is the copy
be advised
but you assigned 9 to x
var y = x
copies it if its a structshouldn't then pie also be 9?
if its an original
ive never changed the value of pie
so you created pie assigned it 3.14
created x assigned it 9
thats a version of pie
yes
and made y thats a copy of x
i made y thats a copy of x before i changed x's value
oohh right
so if it'd be lower both'd be 9 right?
TheRanger#3357
REPL Result: Success
Console Output
Compile: 652.569ms | Execution: 33.387ms | React with โ to remove this embed.
yes
ok Im kinda getting it
so "=" in value types copies variables
pretty much
and my default thinking of "=" comes from refernce type
might be more clearer in c/C++
everything is a value type there, it becomes a reference type if you put a
*
next to a variableok anyway thank you very much for your time and for reference I genuinely wasn't trolling with that
so you either do class or put * before a variable?
i never said ur trolling
class in C++ isnt a reference type
sorry Im not planning on C++
its just struct with
public and private
access modifiersok so for c# this * doesnt't apply?
only in unsafe context
which is very dangerous, can freeze ur pc if you used it wrong
ok I just won't use * then
ull probably need it sometime in the future
I dont want my pc frozen
well if u used it right it wont freeze
usually only useful if you want ur program to run faster/
or trying to execute some c++ code from a c++ dll
Anyhow I need some time to process and revise all this information but I appreciate your help very very much and thank
np
what the
well its now 1087
will we hit 1k?
we already did
๐
Here's to another 1000!
can at least say its thorough
we conclude what an object is?
i think so
it got very sidetracked, many many times
"What is an object?"
"an object is an instance of a reference type"
"and what is a reference type?"
"well its a type that you get a reference too"
"what is a reference?" etc
and varying levels of abstraction back and forth
just a little bit
He try to learn fast (which is bad)
Im not trying to learn fast, I just want to understand bare basics
Just gonna say, the bare basics does not mean having to understand everything that's going on. Knowing what a reference is is good for more advanced usage, but for the basics it's practically only necessary to know the surface level of it. Getting bogged down with all the tiny little details is just counter-productive.
Sorry
You are not trying to learn fast yet like Thinker said you are trying to understand everything at once
Don't rush into things yet
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.