Organizing source files
Put the price calculation in its own file. The main program calls that function. Save both files in the same folder before running the example.
Simple example
Call a function in another file.
Start2 × 3
Result6
static class Prices { public static int Total(int units, int price) => units * price; }
using System;
Console.WriteLine(Prices.Total(2, 3));
6
Advertisements
Everyday examples
A snack stall sells five juices at two coins each. Use the shared price function to print the amount due.
Start5 juices × 2
Result10
static class Prices { public static int Total(int units, int price) => units * price; }
using System;
Console.WriteLine(Prices.Total(5, 2));
10
Run the example
dotnet new console --output Lesson
cp Program.cs Prices.cs Lesson/
dotnet run --project Lesson