-
Notifications
You must be signed in to change notification settings - Fork 370
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
Comments
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 |
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 horizontalLine = new string('-', 74); It is also best to avoid 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) }; |
Because the 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 }; |
Thank you very much. You are right. I am coming from C world and have those habits still :) |
I agree. I will leave this issue open. |
|
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 )
);
} |
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.
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 forStringBuilder myString = new();
. I noticedarg3
does not exist so went the hard wayThe text was updated successfully, but these errors were encountered: