C#: Structure with use of static fields inside a struct
C# Sharp STRUCTURE: Exercise-2 with Solution
Write a program in C# Sharp to declare a simple structure and use static fields inside a struct.
Sample Solution:-
C# Sharp Code:
using System;
// Declaration of a structure named w3rStruct
struct w3rStruct
{
// Declaration of public static integer variables x and y inside the structure
public static int x = 15; // Initializing x with value 15
public static int y = 25; // Initializing y with value 25
}
class strucExer2
{
public static void Main()
{
// Displaying a message about using static fields inside a structure
Console.Write("\n\nStructure with the use of static fields inside a structure :\n");
Console.Write("-------------------------------------------------------------\n");
// Calculating the sum of static variables x and y within the structure w3rStruct
int sum = w3rStruct.x + w3rStruct.y;
// Displaying the sum of x and y
Console.WriteLine("The sum of x and y is {0}\n", sum);
}
}
Sample Output:
Structure with the use of static fields inside a structure : ------------------------------------------------------------- The sum of x and y is 40
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C# Sharp to declare a simple structure.
Next: Write a program in C# Sharp to create a nested struct to store two data for an employee in an array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/csharp-exercises/structure/csharp-structure-exercise-2.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics