w3resource

C#: 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;

// Declaration of a public structure named newStruct
public struct newStruct
{
    public int m, n; // Public fields 'm' and 'n' within the structure

    // Parameterized constructor for the newStruct
    public newStruct(int pt1, int pt2)
    {
        m = pt1; // Initializing 'm' with the value of pt1
        n = pt2; // Initializing 'n' with the value of pt2
    }
}

// Main class
class strucExer7
{
    // Main method
    static void Main()
    {
        // Displaying a message indicating the usage of default and parameterized constructors in a structure
        Console.Write("\n\nStructure declares using default and parameterized constructors :\n");
        Console.Write("-----------------------------------------------------------------\n");

        // Creating instances of newStruct using default and parameterized constructors
        newStruct myPoint1 = new newStruct(); // Using default constructor (fields initialized to default values)
        newStruct myPoint2 = new newStruct(25, 25); // Using parameterized constructor

        // Displaying the values of fields m and n for myPoint1 and myPoint2 instances
        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:

Flowchart: Structure declare using default and parameterized constructors.

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://www.w3resource.com/csharp-exercises/structure/csharp-structure-exercise-7.php