C# Sharp Exercises: Structure declare using default and parameterized constructors
C# Sharp STRUCTURE : Exercise-7 with Solution
Write a program in C# Sharp to demonstrates structure initialization using both default and parameterized constructors.
Sample Solution:-
C# Sharp Code:
using System;
public struct newStruct
{
public int m, n;
public newStruct(int pt1, int pt2)
{
m = pt1;
n = pt2;
}
}
// Declare and initialize struct objects.
class strucExer7
{
static void Main()
{
Console.Write("\n\nStructure declares using default and parameterized constructors :\n");
Console.Write("-----------------------------------------------------------------\n");
newStruct myPoint1 = new newStruct();
newStruct myPoint2 = new newStruct(25, 25);
Console.Write("\nnewStruct 1: ");
Console.WriteLine("m = {0}, n = {1}", myPoint1.m, myPoint1.n);
Console.Write("newStruct 2: ");
Console.WriteLine("m = {0}, n = {1}", myPoint2.m, myPoint2.n);
Console.WriteLine("\nPress any key to exit.");
Console.ReadKey();
}
}
Sample Output:
Structure declares using default and parameterized constructors : ----------------------------------------------------------------- newStruct 1: m = 0, n = 0 newStruct 2: m = 25, n = 25 Press any key to exit.
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C# Sharp to declares a structure with a property, a method, and a private field.
Next: Write a program in C# Sharp to demonstrates structure ure initialization without using the new operator.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework