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: Method that returns a structure

C# Sharp STRUCTURE: Exercise-10 with Solution

Write a program in C# Sharp to implement a method that returns a structure including calling the method and using its value.

Sample Solution:-

C# Sharp Code:

using System;

public struct sampStru
{
    private double val;
    public double Value
    {
        get { return val; }
        set { val = value; }
    }
    public double Read()
    {
        return double.Parse(Console.ReadLine());
    }
}
public struct Square
{
    sampStru ln;
    sampStru ht;

    public sampStru Length
    {
        get { return ln; }
        set { ln = value; }
    }
    public sampStru Breadth
    {
        get { return ht; }
        set { ht = value; }
    }
    public void newSquare()
    {
        sampStru rct = new sampStru();

        Console.WriteLine("\nInput the dimensions of the Square( equal length and breadth ) : ");
        ln = sqrLength();
        Console.Write("breadth : ");
        ht.Value = rct.Read();
    }
    public sampStru sqrLength()
    {
        sampStru rct = new sampStru();

        Console.Write("length : ");
        rct.Value = rct.Read();
        return rct;
    }
}
public class strucExer10
{
    static void Main()
    {
		Console.Write("\n\nMethod that returns a structure  :\n");
		Console.Write("--------------------------------------\n");
        var Sqre = new Square();
        Sqre.newSquare();
        Console.WriteLine();
        Console.WriteLine("Perimeter and Area of the square :");
        Console.WriteLine("Length:    {0}", Sqre.Length.Value);
        Console.WriteLine("Breadth:    {0}", Sqre.Breadth.Value);
        Console.WriteLine("Perimeter: {0}", (Sqre.Length.Value + Sqre.Breadth.Value) * 2);
        Console.WriteLine("Area:      {0}\n", Sqre.Length.Value * Sqre.Breadth.Value);
    }
}

Sample Output:

Method that returns a structure  :                                                                            
--------------------------------------                                                                        
Input the dimensions of the Square( equal length and breadth ) :                                              
length : 10                                                                                                  
breadth : 20                                                                                                  
     
Perimeter and Area of the square :                                                                            
Length:    10                                                                                                 
Breadth:    20                                                                                                
Perimeter: 60                                                                                                 
Area:      200

Flowchart:

Flowchart: Method that returns a structure.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to insert the information of two books.
Next: C# Sharp DateTime Exercises.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.