# Function in Invoke Code with C#

**URL:** <https://forum.uipath.com/t/function-in-invoke-code-with-c/367514>\
**Category:** Activities\
**Tags:** question, system, activities\
**Created:** [06.Декабрь.2021 15:21:29 UTC](https://forum.uipath.com/t/function-in-invoke-code-with-c/367514 "2021-12-06T15:21:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankitdwivedi0290](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/ankitdwivedi0290/32/199222_2.png) [@ankitdwivedi0290](https://forum.uipath.com/u/ankitdwivedi0290)\
**Post date:** [06.Декабрь.2021 15:21:29 UTC](https://forum.uipath.com/t/function-in-invoke-code-with-c/367514/1 "2021-12-06T15:21:29Z")

</div>

I am trying to write C# code using invoke code activity with C# but when I am creating function it is not working and stating that “No compiled code to run”. Below is the example

try  
{  
GetUserName(userName);  
}  
catch(Exception ex)  
{  
}

Public void GetUserName(string username)  
{  
Console.writeline(username);  
}

Please let me know what is the right way to call function in invoke code activity.

---

<div class="post-metadata">

**Author:** ![Jorge\_Cavalcante](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/jorge_cavalcante/32/100851_2.png) [@Jorge\_Cavalcante](https://forum.uipath.com/u/Jorge_Cavalcante)\
**Post date:** [06.Декабрь.2021 15:38:39 UTC](https://forum.uipath.com/t/function-in-invoke-code-with-c/367514/3 "2021-12-06T15:38:39Z")

</div>

I understand that each Invoke Code Block is a Function or Method, as it has both Input and Output variables, that is, you need to create your code routine inside this Invoke Code Block. As far as I know, UiPath doesn’t offer within this activity the possibility of writing classes, methods or functions.

---

<div class="post-metadata">

**Author:** ![ankitdwivedi0290](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/ankitdwivedi0290/32/199222_2.png) [@ankitdwivedi0290](https://forum.uipath.com/u/ankitdwivedi0290)\
**Post date:** [06.Декабрь.2021 15:55:51 UTC](https://forum.uipath.com/t/function-in-invoke-code-with-c/367514/5 "2021-12-06T15:55:51Z")

</div>

Thank you @Jorge_Cavalcante for replying on my query.  
There is one function which I need to call recursively to get the last ID details.  
Could you please help me how I can achieve this ?

---

<div class="post-metadata">

**Author:** ![pikorpa](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/pikorpa/32/548059_2.png) [@pikorpa](https://forum.uipath.com/u/pikorpa)\
**Post date:** [06.Декабрь.2021 23:55:47 UTC](https://forum.uipath.com/t/function-in-invoke-code-with-c/367514/6 "2021-12-06T23:55:47Z")

</div>

Hey,  
if you have code in C# (class, methods or function) you can use it inside invoke code block.  
Firstly you have to import all namespaces which you use:

 ![image](https://global.discourse-cdn.com/uipath/original/4X/e/2/b/e2b251d092191e6068b9aa218723c9f0aef997ee.png)  
Then you need set all arguments which you transferring between UiPath \<-\> C# code.  
example:  
 ![image](https://global.discourse-cdn.com/uipath/original/4X/5/a/6/5a6f13c780187d389511eedf04d85966083a404c.png)  
The last step is use you code without using any class/function/method.  
 ![image](https://global.discourse-cdn.com/uipath/original/4X/c/0/9/c09e4e0c406a01068e8bae2b5fc353ba41a50dae.png)  
Please get my example where I unhiding rows in Excel file using C# code in ‘invoke code method’:  
[cSharpExample.zip](https://forum.uipath.com/uploads/short-url/kpQSWTeSjPRnSP5RIKJdNX15OIp.zip) (22.5 KB)

---

<div class="post-metadata">

**Author:** ![StefanSchnell](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/stefanschnell/32/173622_2.png) [@StefanSchnell](https://forum.uipath.com/u/StefanSchnell)\
**Post date:** [07.Декабрь.2021 05:29:01 UTC](https://forum.uipath.com/t/function-in-invoke-code-with-c/367514/7 "2021-12-07T05:29:01Z")

</div>

@ankitdwivedi0290

Hello Ankit,  
@Jorge_Cavalcante is right, the C# code, inside a Invoke Code Activitiy, is like a Main method of a class. To meet your requirements you can use [Lambda functions](https://forum.uipath.com/t/tip-how-to-use-lambda-expressions-as-sub-function-replacements-in-invoke-code-activity).

```auto
Action<string> GetUserName = (string username) => {
  Console.WriteLine(username);
};

try {
  GetUserName(userName);
} catch(Exception ex){

}

```

Best regards  
Stefan

---

<div class="post-metadata">

**Author:** ![Jorge\_Cavalcante](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/jorge_cavalcante/32/100851_2.png) [@Jorge\_Cavalcante](https://forum.uipath.com/u/Jorge_Cavalcante)\
**Post date:** [10.Декабрь.2021 17:09:25 UTC](https://forum.uipath.com/t/function-in-invoke-code-with-c/367514/8 "2021-12-10T17:09:25Z")

</div>

Hello, @StefanSchnell

I learned a little more from your answer. I never thought of using Lambda Functions in this scope. I believe I will use the Invoke Code activity a lot more from today.

Thanks.
