C#: Declare a simple structure
Write a program in C# Sharp to declare a simple structure.
Sample Solution:-
C# Sharp Code:
using System;
// Declaration of a structure named w3rStruct
struct w3rStruct
{
public int x; // Declaration of a public integer variable x
public int y; // Declaration of a public integer variable y
}
class strucExer1
{
public static void Main()
{
// Displaying a message about declaring a simple structure
Console.Write("\n\nDeclare a simple structure :\n");
Console.Write("--------------------------------\n");
// Creating an instance of the w3rStruct structure
w3rStruct w3st = new w3rStruct();
// Assigning values to the x and y variables within the structure instance
w3st.x = 15;
w3st.y = 25;
// Calculating the sum of x and y variables within the structure instance
int sum = w3st.x + w3st.y;
// Displaying the sum of x and y
Console.WriteLine("The sum of x and y is {0}\n", sum);
}
}
Sample Output:
Declare a simple structure : -------------------------------- The sum of x and y is 40
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: C# Sharp STRUCTURE Exercises with solution.
Next: Write a program in C# Sharp to declare a simple structure and use of static fields inside a struct.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics