Clean Code oriented library. SimpleConvert adds many method extensions to System.Object so you can perform type conversions in the blink of an eye. Do not even think about the scenarios that you need to consider. Let SimpleConvert do it for you.
By just referencing this NuGet package in your project you are done. Now all of you objects will have methods with the signature AsXXX() where XXX is the type you want to transform to.
- Christian Guzman - Main Contributor
This project is licensed under the MIT License - see the LICENSE.md file for details
This library adds the methods with the signature AsXXX to all objects for you to convert to all primitive types and string. Examples of these are:
- AsInt()
- AsString()
- AsBoolean()
- AsLong()
- AsShort()
- AsUShort()
- And so on...
And we also have nullable versions
- AsIntNullable()
- AsBooleanNullable()
- AsLongNullable()
- And so on...
Only AsStringNullable has a special case which we will see in the examples.
myObj.AsInt();
This is the same as doing
Convert.ToInt32(myObj);
myObj.AsIntNullable();
In this case, if myObj is null then we get null, otherwise we get the value as integer.
For common scenarios you will just get the ToString() of the object.
myObj.AsString();
But let's look at this one...
string myStr = null;
myStr.AsString();
myStr.AsStringNullable();
On the first case we will actually get "" (String.Empty). On the second case we will get null.
Float and Single are the same type on C#. However, you have all four methods:
- AsFloat() is exactly the same as AsSingle()
- AsFloatNullable() is exactly the same as AsFloatNullable()
There are really no additional complexities for the other types, but I will keep adding examples just for completion.
- Started this project.