w3resource

C# Sharp STRUCTURE : Exercises, Practice, Solution

C# Sharp STRUCTURE [10 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.  Go to the editor]

1. Write a program in C# Sharp to declare a simple structure.
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 static fields inside a struct.
Expected Output :

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

Click me to see the solution

3. Write a C# Sharp program to create a nested struct that stores two data for an employee.
Test Data:

Create a nested structure 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 C# Sharp program to create a structure, assign a value to it, and call it.
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 that shows what happens when a structure and a class instance are passed to a method.
Expected Output:

When a structure 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 that declares a struct with a property, a method, and a private field.
Expected Output:

Declares a structure 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 structure initialization using both default and parameterized constructors.
Expected Output:

Structure 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 structure initialization without using the new operator.
Expected Output :

Structure 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 two books' information.
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. This includes calling the method and using its value.
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 !



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/index.php