C
C#β€’2y ago
Soinagie

❔ 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
Angius
Angiusβ€’2y ago
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
Soinagie
Soinagieβ€’2y ago
So it's just a variable?
Angius
Angiusβ€’2y ago
No
Soinagie
Soinagieβ€’2y ago
Like x=10?
Angius
Angiusβ€’2y ago
A variable is the object's address, you could say
Soinagie
Soinagieβ€’2y ago
What address
Angius
Angiusβ€’2y ago
How you refer to that specific object Like, you have a Car. But which car do you mean?
Soinagie
Soinagieβ€’2y ago
I still don't know what objects are
Angius
Angiusβ€’2y ago
var myCar = new Car();
var yourCar = new Car();
var myCar = new Car();
var yourCar = new Car();
Soinagie
Soinagieβ€’2y ago
So Im not sure I understand
Angius
Angiusβ€’2y ago
Two different objects, two different cars So each has a different name There's my car, and there's your car
Soinagie
Soinagieβ€’2y ago
And they're both new cars?
Angius
Angiusβ€’2y ago
They're both cars
Soinagie
Soinagieβ€’2y ago
New cars
Angius
Angiusβ€’2y ago
new is how you instantiate an object in C#
Soinagie
Soinagieβ€’2y ago
What
Angius
Angiusβ€’2y ago
How you create a new instance of a car A new instance of an object
Soinagie
Soinagieβ€’2y ago
Idk even know what instantiate means😭
Angius
Angiusβ€’2y ago
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
Soinagie
Soinagieβ€’2y ago
Why make new instance of a car?
Angius
Angiusβ€’2y ago
A specific, existing, physical object Because you want an actual car Not just an idea of a car
Soinagie
Soinagieβ€’2y ago
You already have it just copy it
Angius
Angiusβ€’2y ago
What if both have different colors?
Soinagie
Soinagieβ€’2y ago
But you didn't write that anywhere
Angius
Angiusβ€’2y ago
var myCar = new Car("Ford", "Blue");
var yourCar = new Car("Mazda", "Green");
var myCar = new Car("Ford", "Blue");
var yourCar = new Car("Mazda", "Green");
Soinagie
Soinagieβ€’2y ago
Im confused
Angius
Angiusβ€’2y ago
And even in the case of
var myCar = new Car();
var yourCar = new Car();
var myCar = new Car();
var yourCar = new Car();
what if I want to start just a specific car?
myCar.Start();
myCar.Start();
I don't want to start the idea of a car I want to start specifically my own car
Soinagie
Soinagieβ€’2y ago
What idea?
Pobiega
Pobiegaβ€’2y ago
Are two black fords the same car? no, they are two different instances of a black ford
Angius
Angiusβ€’2y ago
The idea of a thing, the concept of it, not the specific given thing
Soinagie
Soinagieβ€’2y ago
What?
Angius
Angiusβ€’2y ago
"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
Soinagie
Soinagieβ€’2y ago
But only if you put that color in parentheses
Angius
Angiusβ€’2y ago
That's a constructor You can just... not have a constructor, and it can still work
Soinagie
Soinagieβ€’2y ago
Sorry I have no idea about the wording and in general Im just lost
Angius
Angiusβ€’2y ago
var myCar = new Car();
var yourCar = new Car();

myCar.Color = "Red";
yourCar.Color = "White";
var myCar = new Car();
var yourCar = new Car();

myCar.Color = "Red";
yourCar.Color = "White";
Here, I just set the colour of those cars after creating them
Soinagie
Soinagieβ€’2y ago
You just called myCar a variable Then isnt it a variable?
Angius
Angiusβ€’2y ago
The variable gives that object a name
Soinagie
Soinagieβ€’2y ago
So objects are variables
Angius
Angiusβ€’2y ago
var Soinahie = new Person();
var Soinahie = new Person();
No Are you your name? Or are you a person, that has a name we can all use to refer to you?
var Pobiega = new Person();
var Pobiega = new Person();
Two different people, with two different names
Soinagie
Soinagieβ€’2y ago
What pobiega?
Angius
Angiusβ€’2y ago
@Pobiega Another user
Pobiega
Pobiegaβ€’2y ago
πŸ‘‹
Soinagie
Soinagieβ€’2y ago
I dont understand
Angius
Angiusβ€’2y ago
You don't understand a difference between a person and their name? We use names if we want to refer to specific people
Soinagie
Soinagieβ€’2y ago
But what about my name?
Pobiega
Pobiegaβ€’2y ago
What about it?
Angius
Angiusβ€’2y ago
It's how others can tell you apart from other people? It's how others can refer to you?
Soinagie
Soinagieβ€’2y ago
So its indetificator for an object?
Angius
Angiusβ€’2y ago
Yep
Pobiega
Pobiegaβ€’2y ago
in programming we say that "a variable points to an object"
Soinagie
Soinagieβ€’2y ago
And its a variable
Pobiega
Pobiegaβ€’2y ago
no its two different things. One points to the other
Soinagie
Soinagieβ€’2y ago
They just used var
Pobiega
Pobiegaβ€’2y ago
a name points to a person var variableName = new Person(); var variableName is indeed a variable but new Person() is not
Soinagie
Soinagieβ€’2y ago
Yeah so my identuficator is a variable
Pobiega
Pobiegaβ€’2y ago
yes
Soinagie
Soinagieβ€’2y ago
So my name is var
Pobiega
Pobiegaβ€’2y ago
no, var is a keyword variableName is the name
Soinagie
Soinagieβ€’2y ago
I meant short for a varaible
Pobiega
Pobiegaβ€’2y ago
okay, then yes
Soinagie
Soinagieβ€’2y ago
My name is a variable string Or something like that
Pobiega
Pobiegaβ€’2y ago
Soinagie is a "variable" that points to an object discord user
Soinagie
Soinagieβ€’2y ago
And 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?
Pobiega
Pobiegaβ€’2y ago
its not a variable, its not a method. its an object its its own term an object is an instance of a type
Soinagie
Soinagieβ€’2y ago
Type of what?
Pobiega
Pobiegaβ€’2y ago
a type. string is a type for example
Monsieur Wholesome
Monsieur Wholesomeβ€’2y ago
Not 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.)
int result1 = MultiplyTimesTwo(1); // with this i put the value 1 into the "x" variable
int result2 = MultiplyTimesTwo(5); // with this i put the value 5 into the "x" variable
int result3 = MultiplyTimesTwo(999); // with this i put the value 999 into the "x" variable

int MutipleTimesTwo(int x) // x is a variable and a placeholder for whatever value it might be
{
return x * 2;
}
int result1 = MultiplyTimesTwo(1); // with this i put the value 1 into the "x" variable
int result2 = MultiplyTimesTwo(5); // with this i put the value 5 into the "x" variable
int result3 = MultiplyTimesTwo(999); // with this i put the value 999 into the "x" variable

int MutipleTimesTwo(int x) // x is a variable and a placeholder for whatever value it might be
{
return x * 2;
}
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
class Person
{
public int Age;
public string FirstName;
public string LastName;
}
class Person
{
public int Age;
public string FirstName;
public string LastName;
}
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
Person wholesomePerson = new Person();
Person wholesomePerson = new Person();
That specific variable wholesomePerson now contains an object of type Person
Pobiega
Pobiegaβ€’2y ago
int, Β΄Car` etc
Soinagie
Soinagieβ€’2y ago
Its a variable
Pobiega
Pobiegaβ€’2y ago
what is? a type? no
Soinagie
Soinagieβ€’2y ago
String
Pobiega
Pobiegaβ€’2y ago
no, thats type
Soinagie
Soinagieβ€’2y ago
And objects are types of what then?
Pobiega
Pobiegaβ€’2y ago
string myString;
string myString;
thats a variable called "myString" and the type is string objects are instances of types
Soinagie
Soinagieβ€’2y ago
What does that mean?
Pobiega
Pobiegaβ€’2y ago
thats what we just discussed with the cars and chairs
circles.png
circles.pngβ€’2y ago
objects are not types
Soinagie
Soinagieβ€’2y ago
.
Pobiega
Pobiegaβ€’2y ago
var myCar = new Car();
var yourCar = new Car();

var thirdCar = myCar;
var myCar = new Car();
var yourCar = new Car();

var thirdCar = myCar;
here, we have 3 variables, but only 2 objects thirdCar and myCar both point to the same car
Soinagie
Soinagieβ€’2y ago
I very much appriciate your help but I doubt I'll understand any of this
circles.png
circles.pngβ€’2y ago
objects are instances of types
Soinagie
Soinagieβ€’2y ago
Why is thirdCar treated like an object?
Pobiega
Pobiegaβ€’2y ago
what do you mean by that its not?
Soinagie
Soinagieβ€’2y ago
Its just a copy of myCar
Pobiega
Pobiegaβ€’2y ago
no, its not a copy
circles.png
circles.pngβ€’2y ago
it is not a copy
Soinagie
Soinagieβ€’2y ago
Its the same
Pobiega
Pobiegaβ€’2y ago
its a new variable, pointing to the same car
circles.png
circles.pngβ€’2y ago
pointing = meaning third car is another name for my car
Soinagie
Soinagieβ€’2y ago
There is only newCar and all of these variables refer to it
Angius
Angiusβ€’2y ago
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
Soinagie
Soinagieβ€’2y ago
What does pointing= meaning mean?
circles.png
circles.pngβ€’2y ago
new <space> Car creates a new car
Pobiega
Pobiegaβ€’2y ago
okay, lets slow down a bit
var myCar = new Car("black");
var sameCar = myCar;

sameCar.Color = "blue";
Console.WriteLine(myCar.Color);
var myCar = new Car("black");
var sameCar = myCar;

sameCar.Color = "blue";
Console.WriteLine(myCar.Color);
circles.png
circles.pngβ€’2y ago
both variables mean the same thing
Pobiega
Pobiegaβ€’2y ago
what do you think this snippet of code will print?
Soinagie
Soinagieβ€’2y ago
There are no "meaning" and "pointing" variables It'll print same car blue?
Pobiega
Pobiegaβ€’2y ago
Look at the last line of code Console.WriteLine(myCar.Color);
Soinagie
Soinagieβ€’2y ago
Yeah myCar = sameCar So it'll print sameCar With color blue
Pobiega
Pobiegaβ€’2y ago
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
MODiX
MODiXβ€’2y ago
Pobiega#2671
REPL Result: Success
var myCar = new Car("black");
var sameCar = myCar;
sameCar.Color = "blue";
Console.WriteLine(myCar.Color);
public class Car
{
public Car(string color) => Color = color;
public string Color { get; set; }
}
var myCar = new Car("black");
var sameCar = myCar;
sameCar.Color = "blue";
Console.WriteLine(myCar.Color);
public class Car
{
public Car(string color) => Color = color;
public string Color { get; set; }
}
Console Output
blue
blue
Compile: 732.596ms | Execution: 47.060ms | React with ❌ to remove this embed.
Soinagie
Soinagieβ€’2y ago
Why did you put black there? If it isnt used
Pobiega
Pobiegaβ€’2y ago
to have an initial value
Soinagie
Soinagieβ€’2y ago
Why
Pobiega
Pobiegaβ€’2y ago
I wanted to demonstrate that they are the same object
Soinagie
Soinagieβ€’2y ago
You give it a value before you print it
Pobiega
Pobiegaβ€’2y ago
yes
Soinagie
Soinagieβ€’2y ago
And Car and myCar are different things?
Pobiega
Pobiegaβ€’2y ago
yes, Car is a type, and myCar is a variable what about this code then:
var myCar = new Car("black");
var sameCar = new Car("blue");
sameCar.Color = "red";
Console.WriteLine(myCar.Color);
var myCar = new Car("black");
var sameCar = new Car("blue");
sameCar.Color = "red";
Console.WriteLine(myCar.Color);
Soinagie
Soinagieβ€’2y ago
I thought Car was an object
Pobiega
Pobiegaβ€’2y ago
no, Car is a type.
Soinagie
Soinagieβ€’2y ago
I wanted to know about objects
Pobiega
Pobiegaβ€’2y ago
objects are instances of types you create an instance by using the new keyword
Soinagie
Soinagieβ€’2y ago
And I still don't know what that means
Pobiega
Pobiegaβ€’2y ago
so new Car() creates an object
Akseli
Akseliβ€’2y ago
object is an instance of a class
Soinagie
Soinagieβ€’2y ago
But newCar is a variable
Pobiega
Pobiegaβ€’2y ago
yes
Akseli
Akseliβ€’2y ago
class is a specific blueprint for the objects layout
Soinagie
Soinagieβ€’2y ago
So it isnt an object
Pobiega
Pobiegaβ€’2y ago
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"
Soinagie
Soinagieβ€’2y ago
It'll print black I guess
Pobiega
Pobiegaβ€’2y ago
correct.
Soinagie
Soinagieβ€’2y ago
Could you please not use newCar as there already is word "new" being used and it just confuses me
Pobiega
Pobiegaβ€’2y ago
you're the one who used it I didn't. ^ you created it, I just went with it πŸ™‚
Soinagie
Soinagieβ€’2y ago
Bc I thought newCar and new Car are thevsame thing
Pobiega
Pobiegaβ€’2y ago
no ofc not especially not since new Car isnt valid new Car() is the parenthesis is not optional
Soinagie
Soinagieβ€’2y ago
Ok can we take it really slow please?
Pobiega
Pobiegaβ€’2y ago
sure
Soinagie
Soinagieβ€’2y ago
What is a type?
Pobiega
Pobiegaβ€’2y ago
You know how computers only actually work with 1s and 0s?
Soinagie
Soinagieβ€’2y ago
Yeah
Pobiega
Pobiegaβ€’2y ago
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
Soinagie
Soinagieβ€’2y ago
What object?
Pobiega
Pobiegaβ€’2y ago
an object no actual object yet, a theoretical one
Soinagie
Soinagieβ€’2y ago
And what is it?
Akseli
Akseliβ€’2y ago
are you guys trolling
Soinagie
Soinagieβ€’2y ago
Who?
Akseli
Akseliβ€’2y ago
an object is an instance of a class
Soinagie
Soinagieβ€’2y ago
And I dont know what instance of a class is
Angius
Angiusβ€’2y ago
We went through it at the start You need some patience in this case, we're starting from ones and zeroes lol
Pobiega
Pobiegaβ€’2y ago
types are... well, defined types that the computer knows about, because we told it what they are string, int, or Car in our case
Soinagie
Soinagieβ€’2y ago
Car isnt the same as string and int
Pobiega
Pobiegaβ€’2y ago
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
public class Car
{
public string Color { get; set; }
}
public class Car
{
public string Color { get; set; }
}
Soinagie
Soinagieβ€’2y ago
Sorry idk what get set are
Pobiega
Pobiegaβ€’2y ago
ignore it
Soinagie
Soinagieβ€’2y ago
I just wanted to learn from the top so I thought I should know what objects are first
Pobiega
Pobiegaβ€’2y ago
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 type
Soinagie
Soinagieβ€’2y ago
So methods have types?
Pobiega
Pobiegaβ€’2y ago
sure public string GetName() { ... } for example thats a method that returns a string
Soinagie
Soinagieβ€’2y ago
Wait a sec Before we speed off
Akseli
Akseliβ€’2y ago
no, types have methods but methods can have return types
Soinagie
Soinagieβ€’2y ago
Is getName a method?
Pobiega
Pobiegaβ€’2y ago
Akseli, you're really not helping at this point So if anyone is trolling, its you.
circles.png
circles.pngβ€’2y ago
yes
Soinagie
Soinagieβ€’2y ago
Ok so we made a string method
Pobiega
Pobiegaβ€’2y ago
a method that returns a string, yep
Soinagie
Soinagieβ€’2y ago
Wait
Pobiega
Pobiegaβ€’2y ago
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
Soinagie
Soinagieβ€’2y ago
It doesnt say anywhere that it returns
Pobiega
Pobiegaβ€’2y ago
sure it does. it MUST
Soinagie
Soinagieβ€’2y ago
Or is it implied
Akseli
Akseliβ€’2y ago
im just making sure no misinformation is fed
Soinagie
Soinagieβ€’2y ago
Since its not a void
Pobiega
Pobiegaβ€’2y ago
it won't compile if it doesnt return a string
circles.png
circles.pngβ€’2y ago
the word string, in that position, is the return type
Pobiega
Pobiegaβ€’2y ago
because we set its return type to string
Soinagie
Soinagieβ€’2y ago
I thought that you had to use "return" keyword For every non-void
Pobiega
Pobiegaβ€’2y ago
you do
Soinagie
Soinagieβ€’2y ago
And you didn't
Pobiega
Pobiegaβ€’2y ago
you see how I left out the body of the method with ... as the placeholder?
circles.png
circles.pngβ€’2y ago
thats in the implementation (code block which is hidden)
Soinagie
Soinagieβ€’2y ago
Im not sure if hiding things for begginers is a good solution but sure
circles.png
circles.pngβ€’2y ago
for brevity
Pobiega
Pobiegaβ€’2y ago
well I wanted you to focus on the important part the type in the method signature
Soinagie
Soinagieβ€’2y ago
What signature?
Pobiega
Pobiegaβ€’2y ago
public string GetName() thats a "method signature"
Soinagie
Soinagieβ€’2y ago
Its a method
Pobiega
Pobiegaβ€’2y ago
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
Soinagie
Soinagieβ€’2y ago
Ok so its creation of a method
Pobiega
Pobiegaβ€’2y ago
yeah
Soinagie
Soinagieβ€’2y ago
and its type is a creation of a method type?
Pobiega
Pobiegaβ€’2y ago
?
Soinagie
Soinagieβ€’2y ago
Or do you mean string
Pobiega
Pobiegaβ€’2y ago
its return type is string
Soinagie
Soinagieβ€’2y ago
So this block is creation of a method that'll return a string
Pobiega
Pobiegaβ€’2y ago
yep and thats important, because we can now know that it wont return an int or a Car it always returns a string.
Soinagie
Soinagieβ€’2y ago
And thats important because?
Pobiega
Pobiegaβ€’2y ago
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 compile
Soinagie
Soinagieβ€’2y ago
I mean you can assign numbers to cars
Pobiega
Pobiegaβ€’2y ago
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
Soinagie
Soinagieβ€’2y ago
But Im getting off track
arion
arionβ€’2y ago
Yes, u can assign numbers to cars, but a number is not a car. so you can have myCar.CarNumber = 69; It expects a car
circles.png
circles.pngβ€’2y ago
the 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
Soinagie
Soinagieβ€’2y ago
Ok and why did you talk about Car and int not being compatible?
Pobiega
Pobiegaβ€’2y ago
because you asked why its important that methods have return types
Soinagie
Soinagieβ€’2y ago
I thought that they have to have them
Pobiega
Pobiegaβ€’2y ago
they do
Soinagie
Soinagieβ€’2y ago
Ok so objects are made up types
Pobiega
Pobiegaβ€’2y ago
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 chairs
Soinagie
Soinagieβ€’2y ago
But there is no myInt or yourint
Pobiega
Pobiegaβ€’2y ago
sure there is
MODiX
MODiXβ€’2y ago
Pobiega#2671
REPL Result: Success
int myInt = 51;
int myInt = 51;
Compile: 347.904ms | Execution: 58.782ms | React with ❌ to remove this embed.
Pobiega
Pobiegaβ€’2y ago
I just made one
Soinagie
Soinagieβ€’2y ago
And its an object?
Pobiega
Pobiegaβ€’2y ago
myInt is a variable myChair was also a variable, technically.
Soinagie
Soinagieβ€’2y ago
It looks like a variable
Pobiega
Pobiegaβ€’2y ago
a variable POINTING TO AN OBJECT
Soinagie
Soinagieβ€’2y ago
And object being... Where does it point?
Pobiega
Pobiegaβ€’2y ago
to somewhere in memory
Soinagie
Soinagieβ€’2y ago
What does even that mean?
Pobiega
Pobiegaβ€’2y ago
new Car() returns a pointer to memory. you store that in a variable and use it
Soinagie
Soinagieβ€’2y ago
So objects are just things you can point towards?
circles.png
circles.pngβ€’2y ago
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
ACiDCA7
ACiDCA7β€’2y ago
reading through this thread here it sounds like trolling but way to dedicated
Soinagie
Soinagieβ€’2y ago
Wow thanks So motivating
Akseli
Akseliβ€’2y ago
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 created
Soinagie
Soinagieβ€’2y ago
Isnt myCar now the same as new Car?
Akseli
Akseliβ€’2y ago
no
Pobiega
Pobiegaβ€’2y ago
new Car() isnt a variable new is a reserved keyword used to create objects
Soinagie
Soinagieβ€’2y ago
But you didnt even name it This object Its just a Car
Akseli
Akseliβ€’2y ago
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
Pobiega
Pobiegaβ€’2y ago
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
Soinagie
Soinagieβ€’2y ago
I thought they were instances
circles.png
circles.pngβ€’2y ago
for example objects in a list can have no name but exist
Pobiega
Pobiegaβ€’2y ago
they are an instance is not the same thing as a variable
Soinagie
Soinagieβ€’2y ago
If they dont have names and variables whats the point of them? All of them are the same Then
Pobiega
Pobiegaβ€’2y ago
no they are not
circles.png
circles.pngβ€’2y ago
they might have different properties
Akseli
Akseliβ€’2y ago
you can have 5 different chairs with different properties like color and size but they are still all chairs
Pobiega
Pobiegaβ€’2y ago
var list = new List<Car>
{
new Car(),
new Car(),
new Car(),
};
var list = new List<Car>
{
new Car(),
new Car(),
new Car(),
};
Akseli
Akseliβ€’2y ago
they follow the same blueprint
Soinagie
Soinagieβ€’2y ago
Yeah and they're all different so they should have different names
Pobiega
Pobiegaβ€’2y ago
"names" isnt a thing
Soinagie
Soinagieβ€’2y ago
So you could separete them
circles.png
circles.pngβ€’2y ago
do you name your chairs?
Akseli
Akseliβ€’2y ago
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
TheRanger
TheRangerβ€’2y ago
i certainly dont name my chairs πŸ˜„
Akseli
Akseliβ€’2y ago
just like variables
Soinagie
Soinagieβ€’2y ago
I mean how will they know which chair am I talking about
Akseli
Akseliβ€’2y ago
you point to the chair
Soinagie
Soinagieβ€’2y ago
I give them names so other person know which one to pick up
circles.png
circles.pngβ€’2y ago
"the second chair from the left"
Soinagie
Soinagieβ€’2y ago
And objects are just "second from the left"?
circles.png
circles.pngβ€’2y ago
which can be "the second item in the list of chairs"
Pobiega
Pobiegaβ€’2y ago
kinda
Angius
Angiusβ€’2y ago
var secondChairFromTheLeft = new Chair(); Sure
Soinagie
Soinagieβ€’2y ago
Yeah you just named it
Akseli
Akseliβ€’2y ago
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
Soinagie
Soinagieβ€’2y ago
So I could use it now This specific object
Pobiega
Pobiegaβ€’2y ago
you could use it before too
var list = new List<Car>
{
new Car("black"),
new Car("black"),
new Car("black"),
};

list[1].Color = "blue";
var list = new List<Car>
{
new Car("black"),
new Car("black"),
new Car("black"),
};

list[1].Color = "blue";
this is fine
Soinagie
Soinagieβ€’2y ago
Before what?
Pobiega
Pobiegaβ€’2y ago
before "giving it a name"
Soinagie
Soinagieβ€’2y ago
Sorry I dont know anything about lists yet
Pobiega
Pobiegaβ€’2y ago
Look you're probably overthinking this by a lot
Soinagie
Soinagieβ€’2y ago
C# is object oriented language and I dont know what these objects are
Pobiega
Pobiegaβ€’2y ago
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 other
Akseli
Akseliβ€’2y ago
hes trolling
Soinagie
Soinagieβ€’2y ago
So for example car.(color) is an object? Or is it a type Or something else
Pobiega
Pobiegaβ€’2y ago
int car.(color) is not valid code so I cant tell
Soinagie
Soinagieβ€’2y ago
Oops Remove int
Pobiega
Pobiegaβ€’2y ago
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
Console.WriteLine(new Car().Color);
Console.WriteLine(new Car().Color);
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 up
Soinagie
Soinagieβ€’2y ago
Why would I make new objects?
Pobiega
Pobiegaβ€’2y ago
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
circles.png
circles.pngβ€’2y ago
to structure data
Pobiega
Pobiegaβ€’2y ago
its how we write code if your application is a car inventory program isnt it useful to keep track of cars?
Soinagie
Soinagieβ€’2y ago
And I still dont know what that means
Servator
Servatorβ€’2y ago
@Soinagie Almost everything is an object in OOP programming
Pobiega
Pobiegaβ€’2y ago
you keep going around in circles
Servator
Servatorβ€’2y ago
Do you know why literally everything has ".ToString()" method ?
Soinagie
Soinagieβ€’2y ago
Wdym?
Akseli
Akseliβ€’2y ago
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
Servator
Servatorβ€’2y ago
Pobiega
Pobiegaβ€’2y ago
I'm starting to agree with you
Servator
Servatorβ€’2y ago
Soinagie
Soinagieβ€’2y ago
If you're not willing to help you don't have to
circles.png
circles.pngβ€’2y ago
but whatever im kinda bored
Servator
Servatorβ€’2y ago
There is an "Object" class @Soinagie Every classes inherits that Object class
TheRanger
TheRangerβ€’2y ago
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 ...
circles.png
circles.pngβ€’2y ago
and the object class has a (virtual?) to string method
circles.png
circles.pngβ€’2y ago
which means all objects will have it
Soinagie
Soinagieβ€’2y ago
So all objects can be strings?
circles.png
circles.pngβ€’2y ago
yes
Pobiega
Pobiegaβ€’2y ago
no
circles.png
circles.pngβ€’2y ago
mb then
Pobiega
Pobiegaβ€’2y ago
all objects can be "turned into" strings
Servator
Servatorβ€’2y ago
all objects are not strings but all strings are objects not the other way around
Soinagie
Soinagieβ€’2y ago
I thought strings were a type
Pobiega
Pobiegaβ€’2y ago
a type. string is a type.
Servator
Servatorβ€’2y ago
classes are types there are primitive types and non-pritimitive types
Soinagie
Soinagieβ€’2y ago
So color is a non-primitive type
Pobiega
Pobiegaβ€’2y ago
string is a type, but an instance of a string is an object
Soinagie
Soinagieβ€’2y ago
So "hello world" is an object?
Pobiega
Pobiegaβ€’2y ago
yes
Servator
Servatorβ€’2y ago
It's a string yes
Akseli
Akseliβ€’2y ago
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)
circles.png
circles.pngβ€’2y ago
system.drawing.color is (i think)
Servator
Servatorβ€’2y ago
Usually you define an object with "new" keyword
MODiX
MODiXβ€’2y ago
Pobiega#2671
REPL Result: Success
Console.WriteLine("hello world".GetType().FullName);
Console.WriteLine("hello world".GetType().FullName);
Console Output
System.String
System.String
Compile: 495.486ms | Execution: 64.641ms | React with ❌ to remove this embed.
Servator
Servatorβ€’2y ago
But C# has a feature called implicit and explicit operator compiler uses that to create a string
Soinagie
Soinagieβ€’2y ago
So if I do int x=100 then x is an object?
Akseli
Akseliβ€’2y ago
stop introducing more and more unrelated things, the valid explanation for objects is here ^
Pobiega
Pobiegaβ€’2y ago
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
Soinagie
Soinagieβ€’2y ago
Simple question do I need to know what objects are?
circles.png
circles.pngβ€’2y ago
yes definitely
Pobiega
Pobiegaβ€’2y ago
not to the level of understanding you seem to seek
Soinagie
Soinagieβ€’2y ago
Then if I cant use objects why do I need to know about them
Pobiega
Pobiegaβ€’2y ago
who said you cant use them
Soinagie
Soinagieβ€’2y ago
.
Pobiega
Pobiegaβ€’2y ago
var x = new Car(); is using an object its just that x isnt the object its a variable pointing to the object
Soinagie
Soinagieβ€’2y ago
But this object was never created
Servator
Servatorβ€’2y ago
[declaretation] = [assignment] [type] [name] = [value]
Soinagie
Soinagieβ€’2y ago
I cant refer to something that doesnt exist
Pobiega
Pobiegaβ€’2y ago
new Car() creates it
Servator
Servatorβ€’2y ago
It creates it ^^
Soinagie
Soinagieβ€’2y ago
So now x is an instance of car?
Pobiega
Pobiegaβ€’2y ago
no, its a variable pointing to an instance of Car
Servator
Servatorβ€’2y ago
Servator
Servatorβ€’2y ago
yes exactly
Pobiega
Pobiegaβ€’2y ago
x is never anything but a variable
Soinagie
Soinagieβ€’2y ago
And whats that instance? Of car
Pobiega
Pobiegaβ€’2y ago
Ok you are trolling
circles.png
circles.pngβ€’2y ago
a lot of ones and zeros
Pobiega
Pobiegaβ€’2y ago
pick up two pens. they are two different instances of "Pen"
Soinagie
Soinagieβ€’2y ago
Yes they are So theyre 2 objects
Pobiega
Pobiegaβ€’2y ago
sure
Soinagie
Soinagieβ€’2y ago
Then where did you use an object? If x isnt an object
Pobiega
Pobiegaβ€’2y ago
creating it is using it
Soinagie
Soinagieβ€’2y ago
And car is class
Pobiega
Pobiegaβ€’2y ago
x.Color is using an object etc
circles.png
circles.pngβ€’2y ago
class is the class in classify or classification
Pobiega
Pobiegaβ€’2y ago
just because x itself isnt the object doesnt mean you cant interact with the object with the variable
Soinagie
Soinagieβ€’2y ago
And car is this object? And its name is x?
Pobiega
Pobiegaβ€’2y ago
go away troll
Soinagie
Soinagieβ€’2y ago
Or is it its value Tbh I dont know the differnce
Servator
Servatorβ€’2y ago
x just refers to an object real object is new Car()
Soinagie
Soinagieβ€’2y ago
So its it name
Servator
Servatorβ€’2y ago
[declaretation] = [assignment] [type] [name] = [value]
Soinagie
Soinagieβ€’2y ago
And you can refer to that specific object
Servator
Servatorβ€’2y ago
yes
Pobiega
Pobiegaβ€’2y ago
(simplification)
Akseli
Akseliβ€’2y ago
imagine doing this to an actual teacher in school holy shit i wish i back in primary school
Servator
Servatorβ€’2y ago
I wish i was curious like him when i start coding
Soinagie
Soinagieβ€’2y ago
Wdym by that?
Servator
Servatorβ€’2y ago
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
Soinagie
Soinagieβ€’2y ago
Both x=10 and 10=x mean the same
Servator
Servatorβ€’2y ago
no = is used for assignment in programming languages (most of them) not for comparison
Soinagie
Soinagieβ€’2y ago
Yeah I assign value 10 to x
Servator
Servatorβ€’2y ago
= means literally get the value (right side of πŸ™‚ and put it in left side of =
MODiX
MODiXβ€’2y ago
Samarichitane#3472
REPL Result: Success
int x = 1;

Console.WriteLine(x);

x = 5;

Console.WriteLine(x);
int x = 1;

Console.WriteLine(x);

x = 5;

Console.WriteLine(x);
Console Output
1
5
1
5
Compile: 522.035ms | Execution: 27.166ms | React with ❌ to remove this embed.
Soinagie
Soinagieβ€’2y ago
So x isnt 10 but 10 is x?
Servator
Servatorβ€’2y ago
what is 10 ?
Soinagie
Soinagieβ€’2y ago
Int
Servator
Servatorβ€’2y ago
Servator
Servatorβ€’2y ago
but "10" doesn't refer to a variable
circles.png
circles.pngβ€’2y ago
left side is always the target
Servator
Servatorβ€’2y ago
Compiler doesn't know is it variable or not It's just an int like you said not reference or an object
TheRanger
TheRangerβ€’2y ago
a human's name can be Soinagie, but a Soinagie's Name can't be a human
Soinagie
Soinagieβ€’2y ago
So I assign value of right thing to the left thing
Servator
Servatorβ€’2y ago
yes
Soinagie
Soinagieβ€’2y ago
And then x is a object new Car
Servator
Servatorβ€’2y ago
yes
Pobiega
Pobiegaβ€’2y ago
(no) if x was the object, then how on earth would var y = x; be valid?
Soinagie
Soinagieβ€’2y ago
And new always means I create an object
TheRanger
TheRangerβ€’2y ago
var tails = new Fox();
var miles = tails;
var tails = new Fox();
var miles = tails;
Pobiega
Pobiegaβ€’2y ago
there is still only 1 object
TheRanger
TheRangerβ€’2y ago
miles and tails refer to the same fox
Soinagie
Soinagieβ€’2y ago
Then whats the point of keyword "new"? Yeah and you can use both interchangibly
Pobiega
Pobiegaβ€’2y ago
but they are not the object they are references to the same object they are not "the name of the object"
TheRanger
TheRangerβ€’2y ago
in a sonic game, when you start a new level, they do new Sonic(); and stuff
Soinagie
Soinagieβ€’2y ago
Reference is a name You refer to me by using my name
Pobiega
Pobiegaβ€’2y ago
so say reference dont say name
Servator
Servatorβ€’2y ago
We access references with name
TheRanger
TheRangerβ€’2y ago
var soinagie = new Human();
soinagie.Jump();
var soinagie = new Human();
soinagie.Jump();
Soinagie
Soinagieβ€’2y ago
Then this specific new human named soinagie jumps
TheRanger
TheRangerβ€’2y ago
yes
Soinagie
Soinagieβ€’2y ago
And soinagie is an instance of class human
TheRanger
TheRangerβ€’2y ago
yes
Soinagie
Soinagieβ€’2y ago
Or type Class or type?
TheRanger
TheRangerβ€’2y ago
a class is a type
Soinagie
Soinagieβ€’2y ago
They're the same or is it like square and rectangle?
Pobiega
Pobiegaβ€’2y ago
all classes are types, not all types are classes
TheRanger
TheRangerβ€’2y ago
yes, a class is one of the subcategories of a type
Soinagie
Soinagieβ€’2y ago
So a type is a kind of value (like color) and class is a template for objects with the same type
TheRanger
TheRangerβ€’2y ago
not sure what you mean a class is a reference type
Soinagie
Soinagieβ€’2y ago
Reference to what?
TheRanger
TheRangerβ€’2y ago
in here for example, when you call Jump on soinagie, it knows that its refering to the human you just created
var tails = new Fox();
var miles = tails;
tails.Jump();
miles.Jump();
var tails = new Fox();
var miles = tails;
tails.Jump();
miles.Jump();
both miles and tails refer to the same fox
Soinagie
Soinagieβ€’2y ago
Yeah
TheRanger
TheRangerβ€’2y ago
so the same fox will jump twice
Soinagie
Soinagieβ€’2y ago
I get it But class is a reference to what
Pobiega
Pobiegaβ€’2y ago
its not a reference, its a "reference type"
Soinagie
Soinagieβ€’2y ago
So its a kind of specific type of value? Like color?
TheRanger
TheRangerβ€’2y ago
its a type of reference type of value is something else
Soinagie
Soinagieβ€’2y ago
Then is it reference type or type of refernce?
TheRanger
TheRangerβ€’2y ago
thats the same thing
Soinagie
Soinagieβ€’2y ago
And what is it? Its a template for creating objects
TheRanger
TheRangerβ€’2y ago
yeah, u can have a single object, and multiple variables refer to the same object, like tails and fox value types cant
Soinagie
Soinagieβ€’2y ago
Isnt refering and containg value essentialy the same? You use that value in both cases
TheRanger
TheRangerβ€’2y ago
idk i dont really focus on these terms
Soinagie
Soinagieβ€’2y ago
You just gave me a link to refernce types which separetes these two
TheRanger
TheRangerβ€’2y ago
integer is a value type
Soinagie
Soinagieβ€’2y ago
Tails is a refernce type then?
TheRanger
TheRangerβ€’2y ago
Fox is
Soinagie
Soinagieβ€’2y ago
And tails is...
TheRanger
TheRangerβ€’2y ago
a variable
Soinagie
Soinagieβ€’2y ago
What
TheRanger
TheRangerβ€’2y ago
that refers to the object fox you just created
Soinagie
Soinagieβ€’2y ago
I refers to fox Both refer to fox
Pobiega
Pobiegaβ€’2y ago
they both refer to a fox, the same fox. they are not references to the class itself
Soinagie
Soinagieβ€’2y ago
Then tails isnt a variable, its a reference
Akseli
Akseliβ€’2y ago
this is still going? cmon guys
TheRanger
TheRangerβ€’2y ago
tails IS a variable
Soinagie
Soinagieβ€’2y ago
Why?
Akseli
Akseliβ€’2y ago
tails is a reference
Soinagie
Soinagieβ€’2y ago
. .
TheRanger
TheRangerβ€’2y ago
or a reference variable, i guess u can call it that
Akseli
Akseliβ€’2y ago
its both a variable and a reference reference type and a reference are 2 seperate things
Soinagie
Soinagieβ€’2y ago
And same goes for miles
TheRanger
TheRangerβ€’2y ago
yes, reference, and reference type are 2 different things
Soinagie
Soinagieβ€’2y ago
What😭
streepje8
streepje8β€’2y ago
Type = A Car In General Object = A Specific Car Variable = That Car Over There
Soinagie
Soinagieβ€’2y ago
Isnt that car over there also a specific car? I dont think so
streepje8
streepje8β€’2y ago
Its not about the car, its about the pointing to a car a variable is the thing that points to it
Soinagie
Soinagieβ€’2y ago
Yeah you point to a specific car
Akseli
Akseliβ€’2y ago
insanetrollface
TheRanger
TheRangerβ€’2y ago
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
Soinagie
Soinagieβ€’2y ago
In both cases
streepje8
streepje8β€’2y ago
Ok no the object is what you are pointing to and you are the variable you are not a car
Soinagie
Soinagieβ€’2y ago
How am I relevant?
streepje8
streepje8β€’2y ago
If you are pointing to a car you are a sort of variable
Akseli
Akseliβ€’2y ago
one person go at a time and try to explain it, so there wont be any conflicts introduced to his mind
streepje8
streepje8β€’2y ago
(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
Pobiega
Pobiegaβ€’2y ago
we've been trying to explain that for the last 2 hours
Dusty
Dustyβ€’2y ago
and it's very entertaining (no offense with that, I like it that you guys try your best)
Akseli
Akseliβ€’2y ago
he is trolling but i enjoy watching it
Soinagie
Soinagieβ€’2y ago
They* And Im not I just want to understand that
streepje8
streepje8β€’2y ago
Im not sure but i dont want to be rude to them if im wrong
Dusty
Dustyβ€’2y ago
Idk actually, I've seen other people that don't understand it for weeks until a bit of practical experience
streepje8
streepje8β€’2y ago
Ill just leave this convo and read back what came out of it haha
Akseli
Akseliβ€’2y ago
scroll up and you will see
Soinagie
Soinagieβ€’2y ago
Ok @implicit electron can you explain to me varaiables and objects then?
Dusty
Dustyβ€’2y ago
If you wanna understand it go and write some programs, after a few days you'll get a better understanding ?
TheRanger
TheRangerβ€’2y ago
ur an object of human, Soinagie is ur variable, when someone calls ur name, we know we are refering to you
Soinagie
Soinagieβ€’2y ago
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
streepje8
streepje8β€’2y ago
I can't wait for the confusion when they figure out that an object's type is object
TheRanger
TheRangerβ€’2y ago
careful, int isn't a reference type 10 isn't an object
Soinagie
Soinagieβ€’2y ago
X is
Dusty
Dustyβ€’2y ago
you need to explain the difference between reference and value types
Soinagie
Soinagieβ€’2y ago
Ok @implicit electron only talk for now please
TheRanger
TheRangerβ€’2y ago
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
Soinagie
Soinagieβ€’2y ago
I dont know the difference
TheRanger
TheRangerβ€’2y ago
take baby steps, fully understand one of them first
Soinagie
Soinagieβ€’2y ago
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?
TheRanger
TheRangerβ€’2y ago
well you have some idea about reference types right? might need to continue to finish learning that first
Soinagie
Soinagieβ€’2y ago
I dont think I have
TheRanger
TheRangerβ€’2y ago
i gave you an example of miles and tails, what did you not understand from that
Soinagie
Soinagieβ€’2y ago
Then whats the difference between variable and refernce type?
TheRanger
TheRangerβ€’2y ago
Human is a reference type
Soinagie
Soinagieβ€’2y ago
I thought human was a class
TheRanger
TheRangerβ€’2y ago
well, class is a reference type same meaning we did mention that above, i think
Soinagie
Soinagieβ€’2y ago
And what specifically is a refence type?
TheRanger
TheRangerβ€’2y ago
didnt we just explain that?
Soinagie
Soinagieβ€’2y ago
.
TheRanger
TheRangerβ€’2y ago
thats a different issue
Dusty
Dustyβ€’2y ago
explaining that is way easier than the concept of reference types though
Soinagie
Soinagieβ€’2y ago
Int is a type
Dusty
Dustyβ€’2y ago
yes
TheRanger
TheRangerβ€’2y ago
Int is a type, but not a reference type its a Value type
Soinagie
Soinagieβ€’2y ago
And reference types are made up types Like color
Pobiega
Pobiegaβ€’2y ago
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 mean
Soinagie
Soinagieβ€’2y ago
Why is programming so hard to learn
TheRanger
TheRangerβ€’2y ago
depends on the person
TotallyJustin
TotallyJustinβ€’2y ago
It’s not
Dusty
Dustyβ€’2y ago
It's all learning by doing imho
Soinagie
Soinagieβ€’2y ago
I mean car(color)
Pobiega
Pobiegaβ€’2y ago
I would highly recommend you get started programming, instead of wondering on the theoretical definition of every single term
Soinagie
Soinagieβ€’2y ago
There is no default color type in C# You create it So its made up
Pobiega
Pobiegaβ€’2y ago
but what if I "made" it like...
enum Color
{
Red,
Blue
}
enum Color
{
Red,
Blue
}
just because I made it doesnt mean its a reference type you can make value types too, if you want
Soinagie
Soinagieβ€’2y ago
And its a variable or refernce type? The code you just made
Pobiega
Pobiegaβ€’2y ago
thats an enum, its neither its a value type
Thinker
Thinkerβ€’2y ago
@Akseli please don't assume people are trolling, it's just rude and disrespectful in the case that the person is genuinely sincere
Soinagie
Soinagieβ€’2y ago
Then how is it relevant?
Akseli
Akseliβ€’2y ago
its not an assumption
Pobiega
Pobiegaβ€’2y ago
because you just claimed all "made up types" are reference types which they are not
Thinker
Thinkerβ€’2y ago
yes it is, where did they state they are trolling
TotallyJustin
TotallyJustinβ€’2y ago
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
MODiX
MODiXβ€’2y ago
TheRanger#3357
REPL Result: Success
public struct Fox
{
public int Speed;

public Fox()
{
Speed = 0;
}
}

var tails = new Fox();
var miles = tails;
tails.Speed = 10;
Console.WriteLine(miles.Speed);
public struct Fox
{
public int Speed;

public Fox()
{
Speed = 0;
}
}

var tails = new Fox();
var miles = tails;
tails.Speed = 10;
Console.WriteLine(miles.Speed);
Console Output
0
0
Compile: 620.882ms | Execution: 33.214ms | React with ❌ to remove this embed.
Akseli
Akseliβ€’2y ago
read the convo from start...
Thinker
Thinkerβ€’2y ago
700 messages?
MODiX
MODiXβ€’2y ago
TheRanger#3357
REPL Result: Success
public class Fox
{
public int Speed;

public Fox()
{
Speed = 0;
}
}

var tails = new Fox();
var miles = tails;
tails.Speed = 10;
Console.WriteLine(miles.Speed);
public class Fox
{
public int Speed;

public Fox()
{
Speed = 0;
}
}

var tails = new Fox();
var miles = tails;
tails.Speed = 10;
Console.WriteLine(miles.Speed);
Console Output
10
10
Compile: 569.295ms | Execution: 43.221ms | React with ❌ to remove this embed.
Akseli
Akseliβ€’2y ago
you only need to read the first 100 to see it
TheRanger
TheRangerβ€’2y ago
@Soinagie tell me the difference between the 2 codes i posted above
Thinker
Thinkerβ€’2y ago
if you don't want to help then just... leave this thread
Soinagie
Soinagieβ€’2y ago
Theres a class and struct But I dont know how they're different
TheRanger
TheRangerβ€’2y ago
what about the Console output? why did one print 0 , and the other printed 10?
Soinagie
Soinagieβ€’2y ago
Bc fox wasnt a class? In 1st one
TheRanger
TheRangerβ€’2y ago
yeah, but why
Soinagie
Soinagieβ€’2y ago
I dont know
TheRanger
TheRangerβ€’2y ago
simple this
var miles = tails;
var miles = tails;
Thinker
Thinkerβ€’2y ago
I personally can't find anything that suggests anything other than that this person is just trying to understand.
TheRanger
TheRangerβ€’2y ago
cloned tails so now you have 2 foxes
Soinagie
Soinagieβ€’2y ago
Cloned?
TheRanger
TheRangerβ€’2y ago
like, copied
Soinagie
Soinagieβ€’2y ago
Where
TheRanger
TheRangerβ€’2y ago
here
Soinagie
Soinagieβ€’2y ago
Miles and tails arent foxes
Akseli
Akseliβ€’2y ago
just try to help him, im not bothered anymore
TheRanger
TheRangerβ€’2y ago
they are
Soinagie
Soinagieβ€’2y ago
They're a value for a fox Or a refernce Or something else I dont know
TheRanger
TheRangerβ€’2y ago
since fox is a struct in the first code, the variable isnt a reference anymore
Soinagie
Soinagieβ€’2y ago
I dont know what a struct is really
TheRanger
TheRangerβ€’2y ago
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 class
Soinagie
Soinagieβ€’2y ago
And why does it work with class fox? What does that class do?
TheRanger
TheRangerβ€’2y ago
because class is a reference type, struct is a value type many variables can refer to the same object, struct cant
Soinagie
Soinagieβ€’2y ago
Why does it matter if it was copied?
TheRanger
TheRangerβ€’2y ago
depends on the programmer
Soinagie
Soinagieβ€’2y ago
If it cant reference it Since its not a class
TheRanger
TheRangerβ€’2y ago
do you want it to be copied or not
Soinagie
Soinagieβ€’2y ago
No but it still doesmr matter It cant even get access to it
TheRanger
TheRangerβ€’2y ago
if int is a class
Soinagie
Soinagieβ€’2y ago
You just said that int isnt a class
TheRanger
TheRangerβ€’2y ago
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
Soinagie
Soinagieβ€’2y ago
If what?
TheRanger
TheRangerβ€’2y ago
sounds like ur not good at english, no offense so i believe its hard to communicate with you
Soinagie
Soinagieβ€’2y ago
Ok but what did you mean with that?
MODiX
MODiXβ€’2y ago
TheRanger#3357
if int is a class
Quoted by
<@!105026391237480448> from #what's an object? (click here)
React with ❌ to remove this embed.
Pobiega
Pobiegaβ€’2y ago
note how he said if
TheRanger
TheRangerβ€’2y ago
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 minutes
Soinagie
Soinagieβ€’2y ago
There are no refernces here
Pobiega
Pobiegaβ€’2y ago
thats what the "if" was for if int was a reference type, it would be impractical
Soinagie
Soinagieβ€’2y ago
Where is the "if" in this example? What even does "if" has to do with any of it?
Pobiega
Pobiegaβ€’2y ago
if int was a reference type, changing 5000 in one place would change it in all the places
Soinagie
Soinagieβ€’2y ago
Isnt it salary=5000
Pobiega
Pobiegaβ€’2y ago
if as in the english word if, not the keyword.
Soinagie
Soinagieβ€’2y ago
And you change it to salary =10000 Then is it keyword or english word?
TheRanger
TheRangerβ€’2y ago
i was speaking about the contents of the number itself, not letting the variable point to another number
Soinagie
Soinagieβ€’2y ago
Number is the content 10 is 10 Theres nothing more to it How does variable "point to"?
Pobiega
Pobiegaβ€’2y ago
with value types, they dont with reference types, thats how programming with reference types work
TheRanger
TheRangerβ€’2y ago
say for example you have defined a struct color
Soinagie
Soinagieβ€’2y ago
So strings and classes are reference types, rest of them are value types?
MODiX
MODiXβ€’2y ago
TheRanger#3357
REPL Result: Success
public struct Color
{
public int red;
public int green;
public int blue;
}


var color = new Color { red = 255 };
var anotherColor = color;
color.red = 0;
Console.WriteLine(anotherColor.red);
public struct Color
{
public int red;
public int green;
public int blue;
}


var color = new Color { red = 255 };
var anotherColor = color;
color.red = 0;
Console.WriteLine(anotherColor.red);
Console Output
255
255
Compile: 648.509ms | Execution: 37.902ms | React with ❌ to remove this embed.
TheRanger
TheRangerβ€’2y ago
tell me why that printed 255
MODiX
MODiXβ€’2y ago
TheRanger#3357
REPL Result: Success
public class Color
{
public int red;
public int green;
public int blue;
}


var color = new Color { red = 255 };
var anotherColor = color;
color.red = 64;
Console.WriteLine(anotherColor.red);
public class Color
{
public int red;
public int green;
public int blue;
}


var color = new Color { red = 255 };
var anotherColor = color;
color.red = 64;
Console.WriteLine(anotherColor.red);
Console Output
64
64
Compile: 590.516ms | Execution: 35.456ms | React with ❌ to remove this embed.
Soinagie
Soinagieβ€’2y ago
You asked me similiar question while ago and I still don't know
TheRanger
TheRangerβ€’2y ago
and why that printed 64 because one is struct(a value type) and other is class(a reference type)
Soinagie
Soinagieβ€’2y ago
and I dont know the difference
Pobiega
Pobiegaβ€’2y ago
that is the difference
TheRanger
TheRangerβ€’2y ago
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
Soinagie
Soinagieβ€’2y ago
but another color what defined as 255
TheRanger
TheRangerβ€’2y ago
wdym
Soinagie
Soinagieβ€’2y ago
anotherColor = color
TheRanger
TheRangerβ€’2y ago
in which code?
Soinagie
Soinagieβ€’2y ago
color.red= 0 in 1st
TheRanger
TheRangerβ€’2y ago
whjat about it?
Soinagie
Soinagieβ€’2y ago
so it should print 0
TheRanger
TheRangerβ€’2y ago
why should it, i set color's red to be 0, not anotherColor's red
Soinagie
Soinagieβ€’2y ago
yeah and anotherColor = color so they have the same value
TheRanger
TheRangerβ€’2y ago
it got copied thats how value types work
Soinagie
Soinagieβ€’2y ago
I still dont know what you mean by that
TheRanger
TheRangerβ€’2y ago
when u do anotherColor = color its like u did anotherColor = new Color { red = 255 };
Soinagie
Soinagieβ€’2y ago
yeah they both are 255
TheRanger
TheRangerβ€’2y ago
yeah, however in the first one, i only changed color's red to be 0, anotherColor's red stayed the same
Soinagie
Soinagieβ€’2y ago
but they're now the same thing now essentialy they have the same values
TheRanger
TheRangerβ€’2y ago
at first yes, but changed one of their reds
Soinagie
Soinagieβ€’2y ago
but you did the same with class color and it came out different it shouldn't have
TheRanger
TheRangerβ€’2y ago
exactly why not?
Soinagie
Soinagieβ€’2y ago
bc you set anotherColor as 255
TheRanger
TheRangerβ€’2y ago
because they are the same object
Soinagie
Soinagieβ€’2y ago
and in struct they're not?
TheRanger
TheRangerβ€’2y ago
no it got copied like i said
Soinagie
Soinagieβ€’2y ago
copying doesnt erase a value
TheRanger
TheRangerβ€’2y ago
it didnt erase it where did it erase it?
Soinagie
Soinagieβ€’2y ago
in color.red = 0 but it still printed 255
TheRanger
TheRangerβ€’2y ago
i printed anotherColor's red not color's red
Soinagie
Soinagieβ€’2y ago
and same goes for 2nd code you did the same thing
TheRanger
TheRangerβ€’2y ago
yes, because anotherColor and color refer to the same object in the 2nd code
Soinagie
Soinagieβ€’2y ago
then why use struct class?
TheRanger
TheRangerβ€’2y ago
whats a struct class
Soinagie
Soinagieβ€’2y ago
public struct Color
TheRanger
TheRangerβ€’2y ago
to copy data without affecting the original like u see above
Soinagie
Soinagieβ€’2y ago
I still don't understand this copying thing
TheRanger
TheRangerβ€’2y ago
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
Soinagie
Soinagieβ€’2y ago
maybe, kinda?
TheRanger
TheRangerβ€’2y ago
$refvsvalue
Soinagie
Soinagieβ€’2y ago
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
TheRanger
TheRangerβ€’2y ago
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};
Anton
Antonβ€’2y ago
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 slot
Soinagie
Soinagieβ€’2y ago
if it copies the value shouldn't it then be 255? for 1st one
TheRanger
TheRangerβ€’2y ago
who should be 255?
Soinagie
Soinagieβ€’2y ago
bc it copied the value like you said
TheRanger
TheRangerβ€’2y ago
color or anotherColor?
Soinagie
Soinagieβ€’2y ago
anotherColor got value from color which was 255
TheRanger
TheRangerβ€’2y ago
yeah, it did
Soinagie
Soinagieβ€’2y ago
then if its got that value it should print it
TheRanger
TheRangerβ€’2y ago
it did
Soinagie
Soinagieβ€’2y ago
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
TheRangerβ€’2y ago
MODiX
MODiXβ€’2y ago
TheRanger#3357
REPL Result: Success
public struct Color
{
public int red;
public int green;
public int blue;
}


var color = new Color { red = 255 };
var anotherColor = color;
color.red = 0;
Console.WriteLine(color.red);
Console.WriteLine(anotherColor.red);
public struct Color
{
public int red;
public int green;
public int blue;
}


var color = new Color { red = 255 };
var anotherColor = color;
color.red = 0;
Console.WriteLine(color.red);
Console.WriteLine(anotherColor.red);
Console Output
0
255
0
255
Compile: 682.319ms | Execution: 39.248ms | React with ❌ to remove this embed.
Soinagie
Soinagieβ€’2y ago
but you set anotherColor = color so they're the same they have the same value
TheRanger
TheRangerβ€’2y ago
nope, it got copied theyre not the same
Soinagie
Soinagieβ€’2y ago
= means equals
TheRanger
TheRangerβ€’2y ago
yeah ur point?
Soinagie
Soinagieβ€’2y ago
that equals means these things are the same sorry if Im annoying you
TheRanger
TheRangerβ€’2y ago
2 different objects that have the same red 255
Soinagie
Soinagieβ€’2y ago
x = 5, that means x is equal 5 and nothing else
TheRanger
TheRangerβ€’2y ago
ok in math u have x = 5 and y = x now y and x are 5 right?
Soinagie
Soinagieβ€’2y ago
it cant equal x = 6 right
TheRanger
TheRangerβ€’2y ago
what if lets say 5 = 20 is a thing
Soinagie
Soinagieβ€’2y ago
it cant be how?
Servator
Servatorβ€’2y ago
= for assignment == for comparison we talked about this :/
TheRanger
TheRangerβ€’2y ago
they arent asking for comparison atm
Soinagie
Soinagieβ€’2y ago
you cant assign a number other number
TheRanger
TheRangerβ€’2y ago
mathematically u cant yeah i was giving an example ok so do u know pie 3.14?
Soinagie
Soinagieβ€’2y ago
yeah
TheRanger
TheRangerβ€’2y ago
x = pie and y = pie correct?
Soinagie
Soinagieβ€’2y ago
yes
TheRanger
TheRangerβ€’2y ago
what if i changed pie to 9 instead of 3.14
Soinagie
Soinagieβ€’2y ago
its a constant
TheRanger
TheRangerβ€’2y ago
i know but lets say we can change it
Soinagie
Soinagieβ€’2y ago
ok?
TheRanger
TheRangerβ€’2y ago
programatically what would the value of x and y be?
Soinagie
Soinagieβ€’2y ago
9
TheRanger
TheRangerβ€’2y ago
if pie is a class then yes, if its a struct, then no here is a better example
Soinagie
Soinagieβ€’2y ago
is it all in a value or reference? or outside? like you do pie=3.14 then struct pie=x or all inside struct?
MODiX
MODiXβ€’2y ago
TheRanger#3357
REPL Result: Success
public struct Pie
{
public float Value;

public Pie()
{
Value = 3.14f;
}
}


var x = new Pie();
var y = x;
x.Value = 9f;
Console.WriteLine(x.Value);
Console.WriteLine(y.Value);
public struct Pie
{
public float Value;

public Pie()
{
Value = 3.14f;
}
}


var x = new Pie();
var y = x;
x.Value = 9f;
Console.WriteLine(x.Value);
Console.WriteLine(y.Value);
Console Output
9
3.14
9
3.14
Compile: 590.352ms | Execution: 33.431ms | React with ❌ to remove this embed.
MODiX
MODiXβ€’2y ago
TheRanger#3357
REPL Result: Success
public class Pie
{
public float Value;

public Pie