Conteúdo do Curso
C# Beyond Basics
C# Beyond Basics
Defining & Using a Structure
We define a structure using the following syntax:
index
For-example we can define a structure for storing some student data:
index
Now for storing data we need to create an instance (also called an object) using this structure. A structure is basically a custom datatype, and hence it can be treated as such. So for creating a new object of the structure, we simply use the following syntax:
index
We can create multiple objects of the Student
class, all of which can store data:
index
Each object we created has the set of fields defined in the Student
structure and we can store and access data from those fields using the variableName.fieldName
syntax. For example we can access and output the name
field of student2
:
index
When an empty object is created, the fields take up zero values depending upon their datatypes:
string
- empty string""
;char
- empty character''
;int
-0
;float
-0.0
;bool
-false
.
Therefore, the student2.name
will give an empty output.
We can store data in it using the assignment operator (=
):
index
This way we can store data in all of the three objects:
index
Tudo estava claro?