Dot Net Perlsc# structTop 37 C# Example Pages...

[",rlwrtrth.*aCCST~~}T~~Y(P69766F,C(CXBPFFF8(CP8F4A*CCXS}T~~}T~~","Struct, class."," Structs have different performance from classes. They are used as parameters in methods\u2014this influences the time required to call a method. We test struct parameters.","Example."," Here are two type declarations. The first declaration is a struct type, which by definition inherits from the System.ValueType object. Internally, the struct has only one physical unit, containing two reference values. ","And: ","Second, you can see the class declared, which contains two reference value members.","Tip: ","When used, the two types will be stored in different memory locations (stack and heap).","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","Example type declarations: C#","\n\nstruct ExampleStruct\n{\n public int[] _value1;\n public int[] _value2;\n public ExampleStruct(int[] value1, int[] value2)\n {\n this._value1 = value1;\n this._value2 = value2;\n }\n}\n\nclass ExampleClass\n{\n public int[] _value1;\n public int[] _value2;\n public ExampleClass(int[] value1, int[] value2)\n {\n this._value1 = value1;\n this._value2 = value2;\n }\n}","Example 2."," We can use the struct as a parameter. Here we create an array that is stored as a field in both the struct and class instances. The MethodStruct function receives a struct parameter. The MethodClass function receives a class parameter. ","C# program that uses struct and class parameters","\n\nusing System;\n\nclass Program\n{\n static int MethodStruct(ExampleStruct example)\n {\n return example._value1[0];\n }\n\n static int MethodClass(ExampleClass example)\n {\n return example._value1[0];\n }\n\n static void Main()\n {","\n //\n // Create a new array with value 5.\n //\n ","int[] array1 = new int[1];\n array1[0] = 5;\n Console.WriteLine(\"--- Array element assigned ---\");","\n\n //\n // Create new struct and class.\n //\n ","ExampleStruct example1 = new ","ExampleStruct","(array1, array1);\n ExampleClass example2 = new ","ExampleClass","(array1, array1);","\n\n //\n // Use struct and then class in method as parameters.\n //\n ","int value1 = MethodStruct(example1);\n Console.WriteLine(value1);\n\n int value2 = MethodClass(example2);\n Console.WriteLine(value2);","\n\n //\n // Change the value of the array element.\n //\n ","array1[0] = 10;\n Console.WriteLine(\"--- Array element changed ---\");\n\n int value3 = MethodStruct(example1);\n Console.WriteLine(value3);\n\n int value4 = MethodClass(example2);\n Console.WriteLine(value4);\n }\n}\n\n","Output","\n\n--- Array element assigned ---\n5\n5\n--- Array element changed ---\n10\n10","MethodStruct."," This method receives a strongly-typed value of ExampleStruct type. When you call this method, the struct and all its fields will be physically copied onto the function stack. ","And: ","The runtime can take significant time to copy structs when used as method parameters or even locals.","MethodClass"," receives a strongly-typed value containing a reference to an ExampleClass object. In the C# language, an object is the actual data. A reference is the variable that points to that data. The parameter here is a reference. ","Tip: ","Reference values are basically pointers and are very lightweight and efficient. Instantiating objects is more expensive.","Benchmark."," For the benchmark, we test the Dictionary class's TryGetValue method. When you call methods on a collection like Dictionary, at least one set of parameters will be copied. Large value types such as structs can slow down method calls. ","Dictionary ","dictionary","Data types tested: C#","\n\nstruct A\n{\n public int[] a;\n public int[] b;\n}\n\nclass B\n{\n public int[] a;\n public int[] b;\n}\n\n","Dictionary used to store objects: C#","\n\nvar d1 = new Dictionary<string, A>();\nvar d2 = new Dictionary<string, B>();\nd1.Add(\"a\", new A());\nd2.Add(\"a\", new B());\n\n","Code tested in loop: C#","\n\nA a1;\nif (d1.TryGetValue(\"a\", out a1))\n{\n var x = a1.a;\n}\n\nB b1;\nif (d2.TryGetValue(\"a\", out b1))\n{\n var x = b1.a;\n}\n\n","Results","\n\n100000000 iterations tested.\nAlso tested in reverse order (class then struct).\n\nA) Struct used: ","3721 ms","\nB) Class used: ","3595 ms"," [faster]","We find"," that the performance of using structs as parameters continues to degrade with three and four fields. The class parameter, because it does not become larger, does not degrade as much. ","And: ","This means that when the struct increases the number of fields, each method call such as TryGetValue becomes more expensive.","3 fields","\n\nStruct: ","3907 ms","\nClass: ","3733 ms","\n\n","4 fields","\n\nStruct: ","4803 ms","\nClass: ","3634 ms","Discussion."," A common optimization you can do with structs in your programs is to remove them and turn them into classes. When developing complex programs, you will usually extract methods. When you call methods with structs, performance degrades. ","Tip: ","I recommend only using structs when the design of your application is complete.","And: ","Before adding custom structs, be sure to perform benchmarking that proves structs are beneficial.","Summary."," Structs and classes can be used as parameters in C# programs. We noted how structs are implemented when you use them as parameters, reviewing value types. We also saw a benchmark of how structs perform in methods. ","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAAAxCAMAAABqK1L5AAAAMFBMVEX///+zzvd8rvO/1fj1+P4xiu5tpfFbnfDg6/xIk++ZvvWLtvSmxvbV5Pvr8v3K3PnaFXRJAAAA1ElEQVR4Xu3W7QbEMBBG4Xcm3/3a+7/bVZRlqEI5Vs7vhIcwE+3lQbrP4/kHN2LNfbW06zcAMDTWlFnA2LDMAsbGBwaMJTqwJDqw7HRgpQNLogOdDiwHHZjowEXqflXj7r56H2hmW43AevttMIVeA+qsRyILqE4HKrxyhwG3sO1gQPt34AQ6HJgHfMxsBQ081uBrGKC7txJbAcDbjA7scOAQHGhw4DjgQBMbWMUGtswG1iw0cMsiA5dDwgLbkrJEA/rZYpb2gGMAdRMZOIETOIETOIFfGWpGTAdlDEMAAAAASUVORK5CYII=)","url(data:image/jpeg;base64,/9j/2wBDAAcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwf/wQARCACAAPMDACIAAREAAhEA/8QAOwAAAwEBAQEBAAAAAAAAAAAAAAECAwQFBggQAQADAQEAAgMAAwEAAAAAAAABAhEDEiFhBBNBFDFRYv/aAAwDAAABAAIAAD8A/SIAAAIAGQ0tBmNTNkTYhjTSmzObJmwXjX0n0ym5e4PDxt6Hpz+1exgx0ejiXPFlRYYWN9PWMWaaRYo0wo0AAAAAAAAAAAAAAAAAAAgUyDPSmUzZnNiVENJs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zmrYsTNVRLGap8N8LynyesfA8NsGF4P0yih+WuAeIL0y8lNWxYJoPTCal5bTUvKJoqLMPKsaeTioiqvSIhcQcQpcVRMgxhrSDgYFJAMGQIAAADQYBaNLQYLTAAMjAABAgZAAAkgwAoAjCQQMBSTMDAWA8PFYnSwwZgjABAAAP/9k=)","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAG4AAACCAgMAAADasxWRAAAADFBMVEX/6Nn///+VlZX29va5ksB5AAAASElEQVR4AWPACZj/f0DmjkqOSjKtQgcLyJccZiEE9SOmP8mXHEaBMBoIo4EwGggryJccvvVKKAogTnJUclRyVHLklgmjkqOSAALWyrsxEHLDAAAAAElFTkSuQmCC)"]

["url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIwAAAChAgMAAABonL9FAAAACVBMVEUAcrxtz/b////2X1YeAAAAMklEQVR4Xu3UoREAAAjEMJZkSaZkBBz3ItHVrXxA354azdziGsDnNR4eDDxc4/OAz2sWA8BE5pEbrLAAAAAASUVORK5CYII=)","C","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABJCAMAAABPapnRAAAAMFBMVEX////9amn+z879o6Lx//rj//T/MTH95OP/9PT8ubj/AADj0cTR/+3b6drx9O/W9ORYTpHNAAAEfklEQVR4XtWa23KzOgyF0cFHkvT933Y3wa4QQYPr/RPqdUenhnwjLUUSmYYRs6tinqaQiACAvoVhIAp3q3KOHwmyFsQhMITi5pgf5PNWfoho3ETMHARDBGNxOGZOeU+kAqgOMLyFD4jC3+TIuAZRAWT2u+iewkUcjr8vJa+Alg+PSAQ52iCPbIniBT53yyVUjHDALiCYTflwFQcpSyiZFqH8F0g0R/TCcQjinOOV16HKX0HiKocKCDTB18sC/yVOCwk+XberZ/Vnwt+AxGIHd1vfLIDUuw+IdUBQAtJxSNstVhL6oNVZZ1bqSUcqkSqhkkLuPwhSL4ttYzu9HEKpZpowf84iTmc7TD1ej9IjsMq5HKTDdq5Uu+fTEgG8qh2l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