w3resource
C# Sharp Programming Exercises

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?



New Content: Composer: Dependency manager for PHP, R Programming