C# Beginners Tutorial - 28 - Structs

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Thanks for this! I'm learning C# and I know C++ For all I knew, the only difference between struct and class was that struct declares all members as public by default whereas they'd be private for classes.

But the difference you highlighted is much more important - cheers :)

adamstevens
Автор

The difference between a class and a struct... Structs get cached on the stack when building an instance, Classes are put on the Heap... as far as I know. This video doesn't go over the difference so please correct me if I am wrong. Thanks!

andylinkOFFICIAL
Автор

Erich Lancaster 
Structs are value types not reference types so if you were to do something like

struct MyStruct
{
int number;
}
MyStruct obj = new MyStruct();
obj.number = 5;
 MyStruct obj2 = obj;
obj2.number = 6;
//this will print 5 if struct(value type) it will print 6 if class(reference type)

Definition of objects : Simply things in memory
Definition of reference type variables : variables that point to an object out on the heap.
Definition of instances : things that point to objects such as reference type variables and value type variables(obj and obj2).

There is a reason why people use the words instances and objects interchangeably it's because a instance points to a object so we tend to think technically a instance is an object.

  If MyStruct was a class it would print 6 because both obj and obj2 are both referencing the same object allocated out on the heap. The word reference is used when a instance(obj, obj2) is pointing to an object out on the heap. If it's a struct then obj2 and obj will both allocate memory on the stack. (struct)When i set obj2 to obj it will copy the contained value of obj into a new object so any changes made to obj2 won't affect obj and vice versa because they are two different objects if that makes sense.

wasiimo
Автор

thank you man, you are auwsome, easy explanations.

gamie
Автор

new is a keyword requesting memory from the heap. When you initialise a struct using the new operator, you're allocating it on the heap and not on the stack. It is a reference type.
Furthermore, structs can be really large objects - especially if they contain strings (4 bytes each). Allocating them on the stack would be a really bad idea.

C# should have followed Java's footsteps in getting rid of them. They are largely redundant.

waqqas_the_wicked
Автор

Also value types store actual data, if you copy it you make the copy of the data too. Reference types store only references of objects (like a pointer). Make a class and then struck both with the name Client and one field and use this code to test both: Client c1 = new Client(); c1.name = "Adam"; Client c2 = c1; c2="Bob". In struct c1 will not change because c2 was his copy and it has a new value now. In class c1 will change every time we change c2 because they reference the same object.

benderbg
Автор

Hey bro just an advice fix the sound of your mic and make playlists of diffrent tutorials you have made I like to learn and this is very good keep it up man!

Callmeiso
Автор

Question, how do you create an array of the structure data type you just made with a constructor having at least one parameter? I understand that this is how you turn it into an array : Client[] clientarray= new Client[10]; so that means clientarray has 10 Clients in it right? So what if i want to use the constructor with the parameter "string name" on it in which i input all the names of the client inside the client array, what do i type?

felso
Автор

A Struct just looks like a Class. In which circumstances should I use each?

cadamdo
Автор

do you think c# is a good programming laguage?
which advantages i get if i learn it??

Boomerlance
Автор

how do you get rid of errors in the same place?

deathbloodification
Автор

You didn't mention the most important aspect of structs. They are *value* types. When you create a struct and pass it to a method you are passing a copy to the method. When you create an object from a class and pass it to a method you are passing a copy of the *pointer* only.

The difference is subtle but important. An object can be modified by a method but a struct cannot.

Recommendation: Avoid structs. There's no advantage to them and they can cause you grief if you misunderstand their use.

DanHowardMtl
Автор

So you're saying structs are basically pointless in C#?

ThatGuyNamedBender
Автор

@cadamdo1982 As Adam said struct can inherit only from interface. What he didn't say is that struct is value type and class is a reference type. According to MSDN you should use struct when you're dealing with the data that won't change after the object is created and class is more for data that you intend to change plus when you need stronger inheritance.

benderbg
Автор

Why did we need the button now again? o.O

Wupdog
Автор

i'm not sure if you are talking about structures or classes being allocated on the heap, but structures are actually allocated on the stack.
references:
- tried to post the site but youtube wouldnt alllow me, you can google it.
- book: Microsoft Visual C# 2012 Step by Step


iceman
Автор

I know that setting its name would be easy as clientarray[0]="Bob"; but i would like to set all names in the array during declaration.

felso
Автор

hey ever thought of doing a tutorial about mono develop if you do plzz do i need lots of help on it and i would like a experiences person to show me how to do things on it

pitso
Автор

one thing that I will say is that unless you provide real world examples, all the lessons look the same. I just watched the lesson on abstracts and still have no idea how to pass a "message" since it was not explained (only that you are able to). It would be great if the lessons were more complete not just in terms of what can be done but how the lessons can be applied.

dominik
Автор

Basically, only in the default access specification for the data of that type. In a struct, the members are public access by default--but in a class, they are private by default. You still need a constructor to create either, and each can hold member variables and methods.

tcbetka