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: Create a structure and assign the value and call it

C# Sharp STRUCTURE: Exercise-4 with Solution

Write a program in C# Sharp to create a structure and assign the value and call it.

Sample Solution:-

C# Sharp Code:

using System.IO;  
using System;  
// crate a class
class aNewClass
{  
    public int x;  
    public int y;  
} 
//create a structure 
struct aNewStruc  
{  
    public int x;  
    public int y;  
}  
class strucExer4
{  
    static void Main(string[] args)   
    {  
		Console.Write("\n\nCreate a structure and assign the value and call it :\n");
		Console.Write("---------------------------------------------------------\n"); 	
        aNewClass ClassNum1 = new aNewClass();  
        ClassNum1.x = 75;  
        ClassNum1.y = 95; 
	//	ClassNum2 is ClassNum1 type	
        aNewClass ClassNum2 = ClassNum1;  
        ClassNum1.x = 7500;  
        ClassNum1.y = 9500;  
        Console.WriteLine("\nAssign in Class:       x:{0},   y:{1}", ClassNum2.x, ClassNum2.y);  
        aNewStruc StrucNum1 = new aNewStruc();  
        StrucNum1.x = 750;  
        StrucNum1.y = 950; 
	//	StrucNum2 is StrucNum1 type			
        aNewStruc StrucNum2 = StrucNum1;  
        StrucNum1.x = 75;  
        StrucNum1.y = 95; 
        Console.WriteLine("Assign in Structure:   x:{0},    y:{1}\n\n", StrucNum2.x, StrucNum2.y);  
    }  
}

Sample Output:

Create a structure and assign the value and call it :                                                         
---------------------------------------------------------                                                     
Assign in Class:       x:7500,   y:9500                                                                       
Assign in Structure:   x:750,    y:950 

Flowchart:

Flowchart: Create a structure and assign the value and call it.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to create a nested struct to store two data for an employee in an array.
Next: Write a program in C# Sharp to show what happen when a struct and a class instance is passed to a method.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.