From 16e46dd043d2446def3ab97a029a10c73d8ad3ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TH-N=C3=BCrnberg?= Date: Tue, 12 Dec 2017 17:10:32 +0100 Subject: [PATCH] Call by reference --- .../10CallByReference.csproj | 60 ++++++++++++++++ Prog1_WS2017-18/10CallByReference/App.config | 6 ++ Prog1_WS2017-18/10CallByReference/Program.cs | 68 +++++++++++++++++++ .../Properties/AssemblyInfo.cs | 36 ++++++++++ Prog1_WS2017-18/Prog1_WS2017-18.sln | 6 ++ 5 files changed, 176 insertions(+) create mode 100644 Prog1_WS2017-18/10CallByReference/10CallByReference.csproj create mode 100644 Prog1_WS2017-18/10CallByReference/App.config create mode 100644 Prog1_WS2017-18/10CallByReference/Program.cs create mode 100644 Prog1_WS2017-18/10CallByReference/Properties/AssemblyInfo.cs diff --git a/Prog1_WS2017-18/10CallByReference/10CallByReference.csproj b/Prog1_WS2017-18/10CallByReference/10CallByReference.csproj new file mode 100644 index 0000000..5de77f3 --- /dev/null +++ b/Prog1_WS2017-18/10CallByReference/10CallByReference.csproj @@ -0,0 +1,60 @@ + + + + + Debug + AnyCPU + {1A720D7E-A300-4351-9052-B774416A1BE8} + Exe + Properties + _10CallByReference + 10CallByReference + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Prog1_WS2017-18/10CallByReference/App.config b/Prog1_WS2017-18/10CallByReference/App.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/Prog1_WS2017-18/10CallByReference/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Prog1_WS2017-18/10CallByReference/Program.cs b/Prog1_WS2017-18/10CallByReference/Program.cs new file mode 100644 index 0000000..41d2143 --- /dev/null +++ b/Prog1_WS2017-18/10CallByReference/Program.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace _10CallByReference +{ + class Program + { + static void IncrByValue(int x) + { // Call by Value: Wert wird kopiert + x = x + 1; + Console.WriteLine($"x in Incr {x}"); + } + + static void IncrByRef(ref int x, out int y) + { // Ref: Es ist sichergestellt, dass die Ref-Var. einen initialisierten Wert besitzt + // Out: Es ist nicht sichergestellt, dass die out-Var einen init. Wert besitzt + // ABER: Es wird in der Funktion sichergestellt, dass der out-Var ein + // Wert zugewiesen wird, analog zum return + x = x + 1; + y = x * 2; + Console.WriteLine($"x in Incr-By-Ref {x}, y in Main {y}"); + } + + //static double Gps2Kart(double Laengengrad, double Breitengrad, + // out double x, out double y) + + static void IncrArrayByValue(int[] f) + { + for (int i = 0; i < f.Length; i++) + { + f[i]++; + } + } + static void IncrArrayByRef(ref int[] f) + { + f = new int[] { 100, 200, 300, 400, 500, 600, 700, 800 }; + } + static void Main(string[] args) + { + int x = 10; + int a = 20; + int[] feld = { 10, 20, 30, 40 }; + IncrByValue(x); + IncrByValue(x + 1); // Bei der by-value-Übergabe sind auch arithm. Ausdrücke erlaubt + IncrByRef(ref x, out a); + // IncrByRef(ref x + 1, out a); // Bei der by-ref-Übergabe sind nur Variablen erlaubt + // keine Konstanten, keine arithm. Ausdrücke! + // Übergabe per ref: Variable muss vorher intialisiert worden sein + // Übergabe per out: Variable muss vorher nicht init. werden + Console.WriteLine($"x in Main {x}, a in Main {a}"); + IncrArrayByValue(feld); + foreach (int item in feld) + { + Console.WriteLine(item); + } + Console.WriteLine("-----------"); + IncrArrayByRef(ref feld); + foreach (int item in feld) + { + Console.WriteLine(item); + } + + } + } +} diff --git a/Prog1_WS2017-18/10CallByReference/Properties/AssemblyInfo.cs b/Prog1_WS2017-18/10CallByReference/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..0ee245f --- /dev/null +++ b/Prog1_WS2017-18/10CallByReference/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("10CallByReference")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("10CallByReference")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("1a720d7e-a300-4351-9052-b774416a1be8")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Prog1_WS2017-18/Prog1_WS2017-18.sln b/Prog1_WS2017-18/Prog1_WS2017-18.sln index e3888ef..a0384b2 100644 --- a/Prog1_WS2017-18/Prog1_WS2017-18.sln +++ b/Prog1_WS2017-18/Prog1_WS2017-18.sln @@ -79,6 +79,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "10JaggedArrays", "10JaggedA EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "10WortweiseZerlegung", "10WortweiseZerlegung\10WortweiseZerlegung.csproj", "{375DCCFF-AD56-4BD7-B382-CB3DCBA7B79F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "10CallByReference", "10CallByReference\10CallByReference.csproj", "{1A720D7E-A300-4351-9052-B774416A1BE8}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -225,6 +227,10 @@ Global {375DCCFF-AD56-4BD7-B382-CB3DCBA7B79F}.Debug|Any CPU.Build.0 = Debug|Any CPU {375DCCFF-AD56-4BD7-B382-CB3DCBA7B79F}.Release|Any CPU.ActiveCfg = Release|Any CPU {375DCCFF-AD56-4BD7-B382-CB3DCBA7B79F}.Release|Any CPU.Build.0 = Release|Any CPU + {1A720D7E-A300-4351-9052-B774416A1BE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1A720D7E-A300-4351-9052-B774416A1BE8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1A720D7E-A300-4351-9052-B774416A1BE8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1A720D7E-A300-4351-9052-B774416A1BE8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE -- GitLab