Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solving exercise 2.3 in a different way #58

Open
HoshyarKarimi opened this issue May 15, 2022 · 7 comments
Open

Solving exercise 2.3 in a different way #58

HoshyarKarimi opened this issue May 15, 2022 · 7 comments

Comments

@HoshyarKarimi
Copy link

Hello. I did a lot of thinking, searching and readings online docs and wrote the code differently. I know it's dirty and not optimized but thought it deserves sharing.

using static System.Console;
using System.Collections;
using System.Text;

string[] title = new[] { "Types", "Byte(s) of memory", "Min", "Max" };
string[] dataTypes = { "sbyte", "byte", "short", "ushort", "int", "uint", "long", "ulong", "float", "double", "decimal" };
ArrayList typesSizes = new() { sizeof(sbyte), sizeof(byte), sizeof(short), sizeof(ushort), sizeof(int), sizeof(uint), sizeof(long), sizeof(ulong), sizeof(float), sizeof(double), sizeof(decimal) };
ArrayList minimumValue = new() { sbyte.MinValue, byte.MinValue, short.MinValue, ushort.MinValue, int.MinValue, uint.MinValue, long.MinValue, ulong.MinValue, float.MinValue, double.MinValue, decimal.MinValue };
ArrayList maximumValue = new() { sbyte.MaxValue, byte.MaxValue, short.MaxValue, ushort.MaxValue, int.MaxValue, uint.MaxValue, long.MaxValue, ulong.MaxValue, float.MaxValue, double.MaxValue, decimal.MaxValue };

for (int i = 0; i < 111; i++)
{
    Write("-");
}
WriteLine();

WriteLine(String.Format("{0,-10} {1,-18} {2, 40} {3, 40}", title[0], title[1], title[2], title[3]));

for (int i = 0; i < 111; i++)
{
    Write("-");
}
WriteLine();

StringBuilder myString = new();
for (int index = 0; index < dataTypes.Length; index++)
{
    myString.Append(String.Format("{0,-10} {1,-18} {2, 40} {3, 40}\n", dataTypes[index], typesSizes[index], minimumValue[index], maximumValue[index]));
}
WriteLine(myString);

I wish I had know how to refactor my code and use methods, but I am too beginner. But applied your advices and avoided using var and used target-typed new for StringBuilder myString = new();. I noticed arg3 does not exist so went the hard way

@HoshyarKarimi
Copy link
Author

I wanted to have a method to pass data types and it returns MinValue, MaxValue and sizeof. But wasn't able to do that. I know I shouldn't go ahead you and the book but please help if such things are possible, just for curiosity. Thanks in advance

@markjprice
Copy link
Owner

markjprice commented May 16, 2022

Hi, I'm happy that you are enthusiastic to try alternative code solutions. You will learn about methods and parameters in Chapter 4, so that is why the book solution does not use them (yet). Once you have completed Chapter 4, try to write the methods you want again, and if you're still stuck, add a comment here and I will then help you. :)

A couple of small improvements for your code. You can use the constructor for the string type to create a string of repeating characters. For example, the following code will create a string of 74 dashes:

string horizontalLine = new string('-', 74);

It is also best to avoid ArrayList because it uses System.Object and you do not need to modify the collection after creating it, so replace your following code:

ArrayList typesSizes = new() { sizeof(sbyte), sizeof(byte), sizeof(short), sizeof(ushort), sizeof(int), sizeof(uint), sizeof(long), sizeof(ulong), sizeof(float), sizeof(double), sizeof(decimal) };

With an integer array, as shown in the following code:

int[] typesSizes = new[] { sizeof(sbyte), sizeof(byte), sizeof(short), sizeof(ushort), sizeof(int), sizeof(uint), sizeof(long), sizeof(ulong), sizeof(float), sizeof(double), sizeof(decimal) };

@markjprice
Copy link
Owner

markjprice commented May 16, 2022

Because the MinValue and MaxValue property types are different then you'd have to use object for those, for example:

object[] minimumValue = new object[] { sbyte.MinValue, byte.MinValue, short.MinValue, ushort.MinValue, int.MinValue, uint.MinValue, long.MinValue, ulong.MinValue, float.MinValue, double.MinValue, decimal.MinValue };

@HoshyarKarimi
Copy link
Author

HoshyarKarimi commented May 16, 2022

Thank you very much. You are right. I am coming from C world and have those habits still :)
I apply your improvements. I stuck somewhere else but I do not ask about it because now I know I will learn them in Chapter 4. So please do not close this issue if possible to make it better until I complete that chapter. During searching for this exercise I saw many others copied exactly issues with this exercise, someone were asking about arg3 and etc. I think keeping it open helps newcomers.

@markjprice
Copy link
Owner

I agree. I will leave this issue open.

@HoshyarKarimi
Copy link
Author

HoshyarKarimi commented May 17, 2022

        //
        // Summary:
        //     Writes the text representation of the specified objects, followed by the current
        //     line terminator, to the standard output stream using the specified format information.
        //
        // Parameters:
        //   format:
        //     A composite format string.
        //
        //   arg0:
        //     The first object to write using format.
        //
        //   arg1:
        //     The second object to write using format.
        //
        //   arg2:
        //     The third object to write using format.
        //
        // Exceptions:
        //   T:System.IO.IOException:
        //     An I/O error occurred.
        //
        //   T:System.ArgumentNullException:
        //     format is null.
        //
        //   T:System.FormatException:
        //     The format specification in format is invalid.
        public static void WriteLine(string format, object? arg0, object? arg1, object? arg2);
   I think found out why there was no arg3.

@Dreamoochy
Copy link

Another dirty solution. Please, don't use it in production! :)

using static System.Console;
using System.Runtime.InteropServices;

Type[] types = {
         typeof(sbyte),
         typeof(byte),
         typeof(short),
         typeof(ushort),
         typeof(int),
         typeof(uint),
         typeof(long),
         typeof(ulong),
         typeof(float),
         typeof(double),
         typeof(decimal)
       };

WriteLine( "---------------------------------------------------------------------------" );
WriteLine( "{0,-9}{1,-32}{2,3}{3,31}", "Type", "Byte(s) of Memory", "Min", "Max"         );
WriteLine( "---------------------------------------------------------------------------" );

foreach ( Type type in types )
{
  WriteLine( "{0,-8} {1,-4} {2,30} {3,30}",
    type.Name,
    Marshal.SizeOf( type ),
    type.GetField( "MinValue" )?.GetValue( null ),
    type.GetField( "MaxValue" )?.GetValue( null )
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants