C# Script
This page is relevant for ✅Studio ✅Player
Introduction
With C#, you can very quickly develop a program for the mechatronic model in PLC-Lab 3D Studio. Due to the direct execution of the code and the immediate reaction of the 3D model, you see changes immediately without any waiting time. Program changes can be made even while PLC-Lab 3D Studio is in Run mode. As soon as you click Save, the new code is directly applied to the 3D simulation.
PLC-Lab 3D Studio creates a C# project with all input and output variables on the hard drive. This means: you can start writing the code immediately.
PLC-Lab 3D Studio loads the file Program.cs and uses the class CSharpScript4Model from it. Your code must therefore be contained within this class.
For editing the C# code, we recommend Microsoft Visual Studio Code. This modern and powerful development environment is lightweight, supports C# excellently through extensions like "C# for Visual Studio Code" (with IntelliSense, debugging, syntax checking), and is cross-platform and individually customizable.
Installing and Setting Up Visual Studio Code
Step 1:
Download Microsoft Visual Studio Code: https://code.visualstudio.com/download

Step 2:
After installation, start Visual Studio Code. Install the C# Dev Kit plugin:

Step 3:
Install the .NET SDK to enable syntax checking.
Download: https://dotnet.microsoft.com/en-us/download/dotnet/sdk-for-vs-code

Step 4:
After installation, the list of installed plugins should look like this:

Incorrect lines of code are now highlighted as follows:

When a project is opened for the first time, you are asked whether the project should be trusted. Only when this is confirmed and the project is reloaded is the syntax check available.
In the example (1), bool was mistakenly changed to boopl – an invalid type. The word is therefore marked in red. The file Program.cs is also highlighted in red in the Explorer (2).
Creating a C# Project
Select the device CSharpScript (1) and click the Settings button (2). The settings window opens on the right (3). If no C# project exists yet, a new project can be created with the button (4). If a project already exists, it can be opened through the same dialog.

| Setting | Description |
|---|---|
| CSharpFile | Should always be set to 'Program.cs'. |
| Subfolder | Specify any name for the subfolder within the current project folder. |
| ExecutingIntervalMs | Execution interval of the MainPrg function, in milliseconds. |
| PathCodeEditor | Path to the executable file of Visual Studio Code, e.g.: C:\Users\ User \AppData\Local\Programs\Microsoft VS Code\Code.exe |
When creating the project, suitable variables for inputs and outputs are automatically generated based on the current project.
If you want to create another C# program, enter a new directory name in the Subfolder field. Then click the settings button to the right of the device setting twice. This will close and reopen the settings window – the system will recognize that no C# project exists in this folder yet. You can now create a new project with the button.
Writing a C# Program
Two functions are predefined: StartProgram is called once when starting via the Run button. MainPrg, on the other hand, is executed cyclically during the simulation. In StartProgram, for example, initial values can be set – MainPrg represents the actual control program.
Function StartProgram():
// This method is called once when the program starts
public void StartProgram()
{
ConsoleString = "Program started";
}Function MainPrg():
// Main program loop
void MainPrg()
{
ConsoleString = $"StepNr: {StepNr}";
}With the predefined variable ConsoleString of type string, you can output messages. These appear in the status bar of PLC-Lab 3D Studio:
ConsoleString = $"StepNr: {StepNr}";Explanation of the ConsoleString expression with $
The $ in C# is used to create interpolated strings. It allows you to insert variables or expressions directly into a string by enclosing them in curly braces {}.
Conclusion
The CSharpScript device offers a fast and convenient way to develop the control program.
Advantages at a glance:
- Changes are immediately visible – without delay
- Real-time functional testing of the 3D system
- Presentation of the system with a runnable control program
- Basis for later porting to PLC languages such as SCL