In: Computer Science
C# Structures usage tips, with examples ?
A structure in C# is a composite data type that consists of
elements of other types.
The example program below, student is a structure which comprises
of:
Name - a string datatype
Age - An integer type
Courses - A list of string type
The structure in C# can also contain fields, methods, constants,
constructors, properties, indexers, operators and even other
structure types.
The members of a struct can be accessed by using the dot (.)
operator as showing below. Student s1 is an object of the structure
type Student. Using s1, we can access the member of s1 using (.) as
shown below:
Student s1 = new Student();
s1.setName("John Deo");
s1.Age = 21;
A member of a structure can be private or public or internal,
the structure Student has a public member Age and a private member
Name
private string Name;
public int Age;
A structure can also contain methods, the structure Student has
two methods:
public void setName(string name) {
Name = name;
}
public string getName() {
return Name;
}
A structure can also contain a constructor, but we can keep only
parameterized constructor:
public Student(string name, int age, List<string>
courses)
{
Name = name;
Age = age;
Courses = courses;
}
The C# program below create a struct Student which has:
Name - a string datatype
Age - An integer type
Courses - A list of string type
Then in the driver class we create an object s1 of the struct Student and set all the values of the object, then we print the object values. You can modify the object values as per your requirement:
using System;
using System.Collections.Generic;
namespace Application {
public struct Student
{
private string Name;
public int Age;
public List<string> Courses;
public Student(string name, int age, List<string>
courses)
{
Name = name;
Age = age;
Courses = courses;
}
public void setName(string name) {
Name = name;
}
public string getName() {
return Name;
}
}
class DriverClass {
static void Main(string[] args)
{
Student s1 = new Student();
s1.setName("John Deo");
s1.Age = 21;
List<string> courses = new List<string>();
courses.Add("Computer Application");
courses.Add("Electronics");
s1.Courses = courses;
Console.WriteLine("Student name : " +
s1.getName() + ", Age is " +
s1.Age + " and Courses are :");
foreach (string course in s1.Courses)
{
Console.WriteLine(course);
}
}
}
}
Program screenshot with the output, follow the line numbers for the program:
Output:
Please provide a feedback by clicking on the feedback button
1. using System; 2 using System.Collections.Generic; 4. namespace Application { 6 public struct Student 7- { private string Name; public int Age; public List<string> Courses; public Student(string name, int age, List<string> courses) Name = name; Age = age; Courses = courses; public void setName(string name) { Name = name; public string getName() { return Name; } 25 26 } 28- class DriverClass { 29 'static void Main(string[] args) 31 - Student s1 = new Student(; s1.setName("John Deo"); s1.Age = 21; 34 innut
Student s1 = new Student(); s1.setName("John Deo"); s1.Age = 21; List<string> courses = new List<string>(); courses.Add("Computer Application"); courses.Add("Electronics"); s1. Courses = courses; Console.WriteLine("Student name : " + s1.getName() + ", Age is " + s1. Age + " and Courses are :"); foreach (string course in s1. Courses) Console.WriteLine(course); php
We were unable to transcribe this image