-1

I 'm new to C#. Is there any way to create a function that can change the datatype of a variable to an another datatype and return the changed value.

I don't want to use the built-in Convert class.

For example :

Let's assume that ChangeDataType is the name of the function. It should also be possible to do the same thing with all datatypes.


int a = 5;

float b = ChangeDataType(a, "float"); // 5.0

Thanks.

  • 2
    Well, you could use function from class `Convert`, for example `Convert.ChangeType()` or `Convert.ToSingle()` – Fabjan Jan 01 '19 at 10:16
  • Fabjan, I want create a function like that without using the built-in. –  Jan 01 '19 at 10:19
  • 4
    Well, look at the [source code of the built-in](https://referencesource.microsoft.com/#mscorlib/system/convert.cs,441ea31c17007e78,references), and do the same... – GSerg Jan 01 '19 at 10:24
  • Please refer my below answer - you can use implicit or explicit operators for that. One of the examaple of code is already present on stackoverflow at: [this link](https://stackoverflow.com/questions/1176641/explicit-and-implicit-c-sharp) – Manoj Choudhari Jan 01 '19 at 10:25
  • 2
    Just for curiosity: Why are you avoiding the built-in functions? Unlike in C++, the C# compiler comes with built-in libraries of which you can assume that they are always there. – PMF Jan 01 '19 at 10:56
  • PMF, It is for my learning purposes. I use C# for creating UI apps and Python for creating console base apps. BTW, I am 13 years and I am a member of PSF (Python software foundation) who reports bugs. –  Jan 01 '19 at 11:34
  • GSerg, Can write it as an answer so that I can mark it as an Answer –  Jan 01 '19 at 13:38
  • The comments mention that the answers from @ManojChoudhari aren't what you're looking for. That may be the case, but they are the only answers you should consider. It's *possible* to do something else, but it wouldn't make sense. The whole point of a strongly-typed language is to know your types up front so if you make a mistake you get compiler error, not a runtime error. It's better to work with that, not against it. Otherwise we might as well declare every single variable as `object`. – Scott Hannen Jan 01 '19 at 16:40
  • Scott Hannen and GSerg, Thanks. –  Jan 02 '19 at 01:20

1 Answers1

3

Please note that you cannot change data type of already declared variable. But you can create new variable of desired type and then typecast old variable into new one. If this type casting fails, then invalidcastexception is thrown.

Below are different kinds of typecasting options:

1. Implicit Type casting In this you don't need to use any syntax. For ex:

int i = 10;
float a = i;

2. Explicit Type casting You need to specify the type in which you want to convert to. For ex:

class Test
{
    static void Main()
    {
        double x = 1234.7;
        int a;
        // Cast double to int.
        a = (int)x;
        System.Console.WriteLine(a);
    }
}

// Output: 1234

3. User Defined Casts Using implict and explicit keywords in c#. Please refer

4. Helper Classes There are many classes which provides method to convert data types. One of the important class is - Convert. This class provides a lot of methods. Convert.ToString, Convert.ToInt32, etc. Another example is TryParse methods in int, long, double, DateTime, etc classes. Please refer

Sushil Jadhav
  • 2,217
  • 2
  • 19
  • 33
Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37
  • 1
    This is not a helpful answer. Items 1, 2 and 3 are not what the OP is asking about (they want to provide the target type dynamically). Item 4 is about the `Convert` class that the [OP rejected](https://stackoverflow.com/questions/53994601/is-there-any-way-to-create-a-function-that-can-change-the-data-type-of-a-variabl#comment94826716_53994601) before this answer was posted. – GSerg Jan 01 '19 at 13:05
  • 3
    It may not be the answer the OP wants, but in this case I think it is helpful. The OP is new to C# and is familiar with Python, a dynamically typed language, so part of a helpful answer is directing to how the language works. – Scott Hannen Jan 01 '19 at 16:44