Question

In: Computer Science

C# Structures usage tips, with examples ?

C# Structures usage tips, with examples ?

Solutions

Expert Solution

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


Related Solutions

and examples of turkey in the world in steel structures ?
and examples of turkey in the world in steel structures ?
What are examples of controversial (but legal) usage of the orphan drug law (as it can...
What are examples of controversial (but legal) usage of the orphan drug law (as it can result in huge profits for the drug companies)? drugs already mass marketed for certain diseases still qualified and been approved for orphan drug law patent as they are used to treat rare diseases also a drug can receive multiple orphan drug patents if manufacturer proves that the given drug can be used to treat or cure each and everyone of these diseases certain drugs...
Propose 2 examples of real-life usage of Competitive intelligence. Examples should include problem itself and solution.
Propose 2 examples of real-life usage of Competitive intelligence. Examples should include problem itself and solution.
Find numerical examples of energy usage and thermodynamic computations of efficiency. Use the web to compare...
Find numerical examples of energy usage and thermodynamic computations of efficiency. Use the web to compare the efficiencies of several power production processes. Which are in use and which are planned? Be sure to cite your sources. Wikipedia, while useful, is not a primary source, and you should not cite it. To earn the maximum number of points for this discussion, you must: Post your response of at least 150 words
Describe different types of data structures in Computer Science with examples.
Describe different types of data structures in Computer Science with examples.
(Cyber security) What are your tips for protecting yourself against identity theft? Provide relevant examples to...
(Cyber security) What are your tips for protecting yourself against identity theft? Provide relevant examples to support your suggestions? Please provide your own words. No copy from the internet. 150 to 200 words.
Explain the difference between array and structure based on their usage in C++ programming. Declare a...
Explain the difference between array and structure based on their usage in C++ programming. Declare a structure called studentScore which contains name of student, registration number of students, course code and marks. Declare structure variable named studentCS680 based on the structure in (b) to store fifty (50) students’ data. Write a program that prompts a user to enter data for 50 students in a structure variable declared in (b) and calculate the average mark.
(MUST BE DONE IN C (NOT C++)) Define two different structures at the top of your...
(MUST BE DONE IN C (NOT C++)) Define two different structures at the top of your program. Define each structure with exactly three members (each member has to be a different datatype). You may set them up whichever way you want. Don’t forget your semicolons. - Inside main, declare two structures. One for each of the two structures you defined. - Then, initialize each one of the members manually (the three members of your first structure and the three elements...
PLEASE USE R PROGRAMMING LANGUAGE TO ANSWER THESE EXAMPLES. Also please explain any important reasons/tips for...
PLEASE USE R PROGRAMMING LANGUAGE TO ANSWER THESE EXAMPLES. Also please explain any important reasons/tips for how you coded. Thank you! The Basic Examples: a) If Statements: Write code that generates a random variable x uniformly between 0 and 1. If x is bigger than 0.6, square it; if x is less than 0.3, set it equal to 5, otherwise set x = 0. b) Forloops: Write a program that makes a vector of all zeros of length 5.Then write...
what are Sorted lists and their efficiency? in c++ and data structures
what are Sorted lists and their efficiency? in c++ and data structures
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT