Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Structure initialization without using the new operator

C# Sharp STRUCTURE: Exercise-8 with Solution

Write a program in C# Sharp to demonstrates structure initialization without using the new operator.

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 strucExer8
{
    static void Main()
    {
	Console.Write("\n\nStructure initialization without using the new operator :\n");
	Console.Write("----------------------------------------------------------\n");

        newStruct myPoint;

        myPoint.m = 30;
        myPoint.n = 40;


        Console.Write("\nnewStruct : ");
        Console.WriteLine("m = {0}, n = {1}", myPoint.m, myPoint.n);

        Console.WriteLine("\nPress any key to exit.");
        Console.ReadKey();
    }
}

Sample Output:

Structure initialization without using the new operator :                                                        
----------------------------------------------------------                                                        
newStruct : m = 30, n = 40                                                                                    
 
Press any key to exit.

Flowchart:

Flowchart: Structure initialization without using the new operator

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to demonstrates structure initialization using both default and parameterized constructors.
Next: Write a program in C# Sharp to insert the information of two books.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.