'C# How to make a return function?

So i have a question. I'm trying do make a function witch returns a number, but the problem is that i can't convert int to string. My functions looks like this:

    static string EnemyDmg(EnemyDmg _dmg)
    {
        string result = "";
         int EnemyDmg
             = CharAttack - EnemyDefense;
        if (EnemyDmg < 1)
            EnemyDmg = 4;
        result = EnemyDmg;
        return result;
    }

but it should do this

     int EnemyDmg
          = CharAttack - EnemyDefense;
     if (EnemyDmg < 1)
     EnemyDmg = 4;
     Console.WriteLine(EnemyName + "  takes  " + EnemyDmg + "  Damage");

has anyone an idea?

PS: The 4 is just a random number.



Solution 1:[1]

should be static int EnemyDmg(EnemyDmg _dmg). You should return an int, and convert to string outside the function iif you need that

Anyway, to convert a String s into an int i:

int i = Int32.Parse(s);

to convert an int i into a string s

string s = i.ToString();
string s = ""+i; // more "java-like"

Solution 2:[2]

This question is a bit ambiguous; I'm not sure why you've done it this way.

You can convert a C# integer to a string with the .ToString() method, like this:

int a = 12;
string aString = a.ToString();
Console.WriteLine(a);

https://dotnetfiddle.net/sMC3hU

Solution 3:[3]

    static string toStr(int intInput)
    {
        string str = intInput.ToString();
        return str;
    }

    }

This code will do it for you. There is no need to use if statement as there is no any specific requirement, it will make more complicated code.

or else

you can direct use ToString parameter if there is an user input just refer to the 3rd line.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Exceptyon
Solution 2 Matthew Alltop
Solution 3 Rutvik