Wednesday, July 03, 2024

c # : Tuple Class to store / hold together loosely associated/related items

 Many a  time, you would wonder how to return a pair of objects (not necessarily just 2, but 3,4,5) from a a function and simply creating a new Class to hold together those objects does not make sense / justifiable. 


In such cases, you can make use the Tuple Class in C#, which is  versatile class for the same purpose. 


public (int diameter, double circumference, double area) GetCircleMetaData(int radius) {

    const double PI = 3.1415; var diameter = radius * 2; var circumference = PI * diameter; var area = PI * radius * radius; return (diameter, circumference, area); }


var (d, c, a) = GetCircleMetaData(9);

No comments:

Interview Question Preperation : Find longest subarray whose sum is equal to K

Software Engineering Practice interview question Given a array of N elements. Find the length of Longest Subarray whose sum is equal to give...