w3resource logo
C# Sharp Exercises

C# Sharp programming Exercises, Practice, Solution : STRUCTURE

C# Sharp STRUCTURE [10 exercises with solution]

1. Write a program in C# Sharp to declare a simple structure. Go to the editor
Expected Output:

Declare a simple structure :
-------------------------------- 
The sum of x and y is 40  

Click me to see the solution

2. Write a program in C# Sharp to declare a simple structure and use of static fields inside a struct. Go to the editor
Expected Output :

Structure with the use of static fields inside a struct :                            
---------------------------------------------------------                       
The sum of x and y is 40 

Click me to see the solution

3. Write a program in C# Sharp to create a nested struct to store two data for an employee in an array. Go to the editor
Test Data:

Create a nested struct and store data in an array :                              
-------------------------------------------------------                          
Name of the employee : H.Rana                                                    
Input day of the birth : 05                                                      
Input month of the birth : 02                                                    
Input year for the birth : 58                                                    
                                                                                 
Name of the employee : S.Mathur                                                  
Input day of the birth : 04                                                      
Input month of the birth : 08                                                    
Input year for the birth : 59 

Click me to see the solution

4. Write a program in C# Sharp to create a structure and Assign the Value and call it. Go to the editor
Expected 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 

Click me to see the solution

5. Write a program in C# Sharp to show what happen when a struct and a class instance is passed to a method. Go to the editor
Expected Output:

When a struct and a class instance is passed to a method :                       
--------------------------------------------------------------                   
ns.n = 5                                                                         
nc.n = 8

Click me to see the solution

6. Write a program in C# Sharp to declares a struct with a property, a method, and a private field. Go to the editor
Expected Output:

Declares a struct with a property, a method, and a private field :               
----------------------------------------------------------------------           
The stored value is: 15 

Click me to see the solution

7. Write a program in C# Sharp to demonstrates struct initialization using both default and parameterized constructors. Go to the editor
Expected Output:

Struct declares using default and parameterized constructors :                    
-----------------------------------------------------------------                
newStruct 1: m = 0, n = 0                                                        
newStruct 2: m = 25, n = 25                                                      

Click me to see the solution

8. Write a program in C# Sharp to demonstrates struct initialization without using the new operator. Go to the editor
Expected Output :

Struct initialization without using the new operator :                    
--------------------------------------------------------                
newStruct : m = 30, n = 40                                                       

Click me to see the solution

9. Write a program in C# Sharp to insert the information of two books. Go to the editor
Test Data :

Insert the information of two books :                    
-----------------------------------------                
Information of book 1 :                                  
Input name of the book : BASIC                           
Input the author : S.TROELSTRA                           
                                                         
Information of book 2 :                                  
Input name of the book : C+                              
Input the author : G.RTRTG                               

Expected Output:

1: Title = BASIC,  Author = S.TROELSTRA                  
                                                         
2: Title = C+,  Author = G.RTRTG 

Click me to see the solution

10. Write a program in C# Sharp to implement a method that returns a structure including calling the method and using its value. Go to the editor
Test Data :

Method that returns a structure  :                                               
--------------------------------------                                           
Input the dimensions of the Square( equal length and breadth ) :                 
length : 20                                                                      
breadth : 40                                                                     

Expected Output:

Perimeter and Area of the square :                                               
Length:    20                                                                    
Breadth:    40                                                                   
Perimeter: 120                                                                   
Area:      800 

Click me to see the solution

C# Sharp Code Editor:

More to Come !