The size property in C# is used to get or set the number of elements within a collection. It is a property of different collection classes in .NET framework such as Array, List, Dictionary, and so on. The size property is mostly used to check the length of an array or the number of elements in a list.
Example 1: Getting the Size Property of an Array
int[] numbers = new int[5]; int arraySize = numbers.Length; Console.WriteLine("The size of the array is: " + arraySize);
In this example, an integer array is created with a length of 5. The size property is then used to get the length of the array and assign it to the arraySize variable. The output of the program will display the size of the array (5).
Package/Library: No package/library is required for this example. It is a built-in property of the Array class in C#.
Example 2: Getting the Size Property of a List
List names = new List(); names.Add("John"); names.Add("Jane"); names.Add("Mary"); int listSize = names.Count; Console.WriteLine("The size of the list is: " + listSize);
In this example, a string list is created and three elements are added to it. The size property (Count) is then used to get the number of elements in the list and assign it to the listSize variable. The output of the program will display the size of the list (3).
Package/Library: No package/library is required for this example. It is a built-in property of the List class in C#.
C# (CSharp) Size - 60 examples found. These are the top rated real world C# (CSharp) examples of Size extracted from open source projects. You can rate examples to help us improve the quality of examples.