0

I am coming from Go language and wanted to know if it's possible to add build flags that assign values to variables at build time. For example, in Go we can do something like

// main.go
package main

import "fmt"

var world = ""

func main() {
    fmt.Println("Hello,", world)
}

And using go build -ldflags "-X main.world=world" main.go will output Hello, world. Is there something like this that can be done in C# when using dontnet build or dotnet publish?

What I wanted to do was, have an endpoint in ASP.Net Core that would print out the version number and SHA code. But I want them to come in at build time. I could probably use the appsettings.json but I am not sure if that's the right way or not.

Akshay
  • 2,622
  • 1
  • 38
  • 71
  • Does this answer your question? [msbuild, defining Conditional Compilation Symbols](https://stackoverflow.com/questions/479979/msbuild-defining-conditional-compilation-symbols) – Felix Aug 14 '22 at 04:25
  • you can use conditional compilation, preprocessor directives and other things.... but to be honest this kind of defeats the purpose of compiled languages... since there are more robust ways to do this... appsettings.json is the way to go for runtime constants that my change accross environemnts and deployments – Jonathan Alfaro Aug 14 '22 at 04:26
  • `aspsettings.json` isn't processed at compile time; however, in my opinion printing version and SHA code is better using runtime values anyway. compile-time values are usually for conditional compilation options - but you can twist them into what you want to do as well – Felix Aug 14 '22 at 04:27
  • @Felix ya I though so too. – Akshay Aug 14 '22 at 07:02

0 Answers0