-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoloLearn-csharp-notes.txt
11864 lines (9945 loc) · 370 KB
/
SoloLearn-csharp-notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Classes & Objects
Classes & Objects
Value & Reference Types
Class Example
Encapsulation
Constructors
Properties
Quiz
Module 1 Quiz
Code Project
Social Network
Arrays and Strings
Arrays
Using Arrays in Loops
Multidimensional Arrays
Jagged Arrays
Array Properties & Methods
Working with Strings
Quiz
Module 2 Quiz
Code Project
Words
More On Classes
Destructors
Static Members
Static Classes
this & readonly
Indexers
Operator Overloading
Quiz
Module 3 Quiz
Code Project
Dance
Inheritance & Polymorphism
Inheritance
Protected Members
Derived Class Constructor & Destructor
Polymorphism
Abstract Classes
Interfaces
Nested Classes
Namespaces
Quiz
Module 4 Quiz
Code Project
Drawing Application
Structs, Enums, Exceptions & Files
Structs
Enums
Exception Handling
Working with Files
Module 5 Quiz
Code Project
Robot-barman
Generics
Generic Methods
Generic Classes
Collections
Lists and BitArray
Stack & Queue
Dictionary & HashSet
Module 6 Quiz
Code Project
Coffee Time
######################################################################################
######################################################################################
## endroughxfer-urldump #1#
## <EOF><EOF> Intermediate C# (SoloLearn) Volume 1
######################################################################################
######################################################################################
===================================================================
++=====================++
++=====================++ Classes & Objects:: Classes & Objects
Classes
As we have seen in the previous modules, built-in data types are used to store a single value in a declared variable. For example, int x stores an integer value in a variable named x.
In object-oriented programming, a class is a data type that defines a set of variables and methods for a declared object.
For example, if you were to create a program that manages bank accounts, a BankAccount class could be used to declare an object that would have all the properties and methods needed for managing an individual bank account, such as a balance variable and Deposit and Withdrawal methods.
A class is like a blueprint. It defines the data and behavior for a type. A class definition starts with the keyword class followed by the class name. The class body contains the data and actions enclosed by curly braces.
class BankAccount
{
//variables, methods, etc.
}
C#
The class defines a data type for objects, but it is not an object itself. An object is a concrete entity based on a class, and is sometimes referred to as an instance of a class.
===================================================================
Objects
Just as a built-in data type is used to declare multiple variables, a class can be used to declare multiple objects. As an analogy, in preparation for a new building, the architect designs a blueprint, which is used as a basis for actually building the structure. That same blueprint can be used to create multiple buildings.
Programming works in the same fashion. We define (design) a class that is the blueprint for creating objects.
In programming, the term type is used to refer to a class name: We're creating an object of a particular type.
Once we've written the class, we can create objects based on that class. Creating an object is called instantiation.
An object is called an instance of a class.
===================================================================
Objects
Each object has its own characteristics. Just as a person is distinguished by name, age, and gender, an object has its own set of values that differentiate it from another object of the same type.
The characteristics of an object are called properties.
Values of these properties describe the current state of an object. For example, a Person (an object of the class Person) can be 30 years old, male, and named Antonio.
Objects aren't always representative of just physical characteristics.
For example, a programming object can represent a date, a time, and a bank account. A bank account is not tangible; you can't see it or touch it, but it's still a well-defined object because it has its own properties.
===================================================================
++=====================++
++=====================++ Value & Reference Types
Value Types
C# has two ways of storing data: by reference and by value.
The built-in data types, such as int and double, are used to declare variables that are value types. Their value is stored in memory in a location called the stack.
For example, the declaration and assignment statement int x = 10; can be thought of as:
img-component
The value of the variable x is now stored on the stack.
===================================================================
Reference Types
Reference types are used for storing objects. For example, when you create an object of a class, it is stored as a reference type.
Reference types are stored in a part of the memory called the heap.
When you instantiate an object, the data for that object is stored on the heap, while its heap memory address is stored on the stack.
That is why it is called a reference type - it contains a reference (the memory address) to the actual object on the heap.
img-component
As you can see, the p1 object of type Person on the stack stores the memory address of the heap where the actual object is stored.
Stack is used for static memory allocation, which includes all your value types, like x.
Heap is used for dynamic memory allocation, which includes custom objects, that might need additional memory during the runtime of your program.
===================================================================
++=====================++
++=====================++ Class Example
Example of a Class
Let’s create a Person class:
class Person
{
int age;
string name;
public void SayHi()
{
Console.WriteLine("Hi");
}
}
C#
The code above declares a class named Person, which has age and name fields as well as a SayHi method that displays a greeting to the screen.
You can include an access modifier for fields and methods (also called members) of a class. Access modifiers are keywords used to specify the accessibility of a member.
A member that has been defined public can be accessed from outside the class, as long as it's anywhere within the scope of the class object. That is why our SayHi method is declared public, as we are going to call it from outside of the class.
You can also designate class members as private or protected. This will be discussed in greater detail later in the course. If no access modifier is defined, the member is private by default.
===================================================================
Example of a Class
Now that we have our Person class defined, we can instantiate an object of that type in Main.
The new operator instantiates an object and returns a reference to its location:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
class Person {
int age;
string name;
public void SayHi() {
Console.WriteLine("Hi");
}
}
static void Main(string[] args)
{
Person p1 = new Person();
p1.SayHi();
}
}
}
OUTPUT
Hi
The code above declares a Person object named p1 and then calls its public SayHi() method.
Notice the dot operator (.) that is used to access and call the method of the object.
===================================================================
Example of a Class
You can access all public members of a class using the dot operator.
Besides calling a method, you can use the dot operator to make an assignment when valid.
For example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
class Dog
{
public string name;
public int age;
}
static void Main(string[] args)
{
Dog bob = new Dog();
bob.name = "Bobby";
bob.age = 3;
Console.WriteLine(bob.age);
}
}
}
OUTPUT
3
===================================================================
Encapsulation
Part of the meaning of the word encapsulation is the idea of "surrounding" an entity, not just to keep what's inside together, but also to protect it.
In programming, encapsulation means more than simply combining members together within a class; it also means restricting access to the inner workings of that class.
Encapsulation is implemented by using access modifiers. An access modifier defines the scope and visibility of a class member.
Encapsulation is also called information hiding.
===================================================================
Encapsulation
C# supports the following access modifiers: public, private, protected, internal, protected internal.
As seen in the previous examples, the public access modifier makes the member accessible from the outside of the class.
The private access modifier makes members accessible only from within the class and hides them from the outside.
protected will be discussed later in the course.
===================================================================
Encapsulation
To show encapsulation in action, let’s consider the following example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class BankAccount {
private double balance=0;
public void Deposit(double n) {
balance += n;
}
public void Withdraw(double n) {
balance -= n;
}
public double GetBalance() {
return balance;
}
}
class Program
{
static void Main(string[] args)
{
BankAccount b = new BankAccount();
b.Deposit(199);
b.Withdraw(42);
Console.WriteLine(b.GetBalance());
}
}
}
OUTPUT
157
We used encapsulation to hide the balance member from the outside code. Then we provided restricted access to it using public methods. The class data can be read through the GetBalance method and modified only through the Deposit and Withdraw methods.
You cannot directly change the balance variable. You can only view its value using the public method. This helps maintain data integrity.
We could add different verification and checking mechanisms to the methods to provide additional security and prevent errors.
In summary, the benefits of encapsulation are:
- Control the way data is accessed or modified.
- Code is more flexible and easy to change with new requirements.
- Change one part of code without affecting other parts of code.
===================================================================
Constructors
A class constructor is a special member method of a class that is executed whenever a new object of that class is created.
A constructor has exactly the same name as its class, is public, and does not have any return type.
For example:
class Person
{
private int age;
public Person()
{
Console.WriteLine("Hi there");
}
}
C#
Now, upon the creation of an object of type Person, the constructor is automatically called.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
class Person
{
private int age;
public Person()
{
Console.WriteLine("Hi there");
}
}
static void Main(string[] args)
{
Person p = new Person();
}
}
}
OUTPUT
Hi there
This can be useful in a number of situations. For example, when creating an object of type BankAccount, you could send an email notification to the owner.
The same functionality could be achieved using a separate public method. The advantage of the constructor is that it is called automatically.
===================================================================
Constructors
Constructors can be very useful for setting initial values for certain member variables.
A default constructor has no parameters. However, when needed, parameters can be added to a constructor. This makes it possible to assign an initial value to an object when it's created, as shown in the following example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
class Person
{
private int age;
private string name;
public Person(string nm)
{
name = nm;
}
public string getName()
{
return name;
}
}
static void Main(string[] args)
{
Person p = new Person("David");
Console.WriteLine(p.getName());
}
}
}
OUTPUT
David
Now, when the object is created, we can pass a parameter that will be assigned to the name variable.
Constructors can be overloaded like any method by using different numbers of parameters.
===================================================================
++=====================++
++=====================++ Properties
Properties
As we have seen in the previous lessons, it is a good practice to encapsulate members of a class and provide access to them only through public methods.
A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they actually include special methods called accessors.
The accessor of a property contains the executable statements that help in getting (reading or computing) or setting (writing) a corresponding field. Accessor declarations can include a get accessor, a set accessor, or both.
For example:
class Person
{
private string name; //field
public string Name //property
{
get { return name; }
set { name = value; }
}
}
C#
The Person class has a Name property that has both the set and the get accessors.
The set accessor is used to assign a value to the name variable; get is used to return its value.
value is a special keyword, which represents the value we assign to a property using the set accessor.
The name of the property can be anything you want, but coding conventions dictate properties have the same name as the private field with a capital letter.
===================================================================
Properties
Once the property is defined, we can use it to assign and read the private member:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
static void Main(string[] args)
{
Person p = new Person();
p.Name = "Bob";
Console.WriteLine(p.Name);
}
}
}
The property is accessed by its name, just like any other public member of the class.
===================================================================
Properties
Any accessor of a property can be omitted.
For example, the following code creates a property that is read-only:
class Person
{
private string name;
public string Name
{
get { return name; }
}
}
C#
A property can also be private, so it can be called only from within the class.
===================================================================
Properties
So, why use properties? Why not just declare the member variable public and access it directly?
With properties you have the option to control the logic of accessing the variable.
For example, you can check if the value of age is greater than 0, before assigning it to the variable:
class Person
{
private int age=0;
public int Age
{
get { return age; }
set {
if (value > 0)
age = value;
}
}
}
C#
You can have any custom logic with get and set accessors.
===================================================================
Auto-Implemented Properties
When you do not need any custom logic, C# provides a fast and effective mechanism for declaring private members through their properties.
For example, to create a private member that can only be accessed through the Name property's get and set accessors, use the following syntax:
public string Name { get; set; }
C#
As you can see, you do not need to declare the private field name separately - it is created by the property automatically. Name is called an auto-implemented property. Also called auto-properties, they allow for easy and short declaration of private members.
We can rewrite the code from our previous example using an auto-property:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
class Person
{
public string Name { get; set; }
}
static void Main(string[] args)
{
Person p = new Person();
p.Name = "Bob";
Console.WriteLine(p.Name);
}
}
}
===================================================================
++=====================++
++=====================++ Arrays and Strings:: Arrays
Arrays
C# provides numerous built-in classes to store and manipulate data.
One example of such a class is the Array class.
An array is a data structure that is used to store a collection of data. You can think of it as a collection of variables of the same type.
For example, consider a situation where you need to store 100 numbers. Rather than declare 100 different variables, you can just declare an array that stores 100 elements.
To declare an array, specify its element types with square brackets:
int[ ] myArray;
C#
This statement declares an array of integers.
Since arrays are objects, we need to instantiate them with the new keyword:
int[ ] myArray = new int[5];
C#
This instantiates an array named myArray that holds 5 integers.
Note the square brackets used to define the number of elements the array should hold.
===================================================================
Arrays & Loops
It's occasionally necessary to iterate through the elements of an array, making element assignments based on certain calculations. This can be easily done using loops.
For example, you can declare an array of 10 integers and assign each element an even value with the following loop:
int[ ] a = new int[10];
for (int k = 0; k < 10; k++) {
a[k] = k*2;
}
C#
We can also use a loop to read the values of an array.
For example, we can display the contents of the array we just created:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int[ ] a = new int[10];
for (int k = 0; k < 10; k++) {
a[k] = k*2;
}
for (int k = 0; k < 10; k++) {
Console.WriteLine(a[k]);
}
}
}
}
OUTPUT
0
2
4
6
8
10
12
14
16
18
The variable k is used to access each array element.
The last index in the array is 9, so the for loop condition is k<10.
===================================================================
++=====================++
++=====================++ Using Arrays in Loops
Arrays & Loops
It's occasionally necessary to iterate through the elements of an array, making element assignments based on certain calculations. This can be easily done using loops.
For example, you can declare an array of 10 integers and assign each element an even value with the following loop:
int[ ] a = new int[10];
for (int k = 0; k < 10; k++) {
a[k] = k*2;
}
C#
We can also use a loop to read the values of an array.
For example, we can display the contents of the array we just created:
=================================================================== Arrays & Loops
It's occasionally necessary to iterate through the elements of an array, making element assignments based on certain calculations. This can be easily done using loops.
For example, you can declare an array of 10 integers and assign each element an even value with the following loop:
int[ ] a = new int[10];
for (int k = 0; k < 10; k++) {
a[k] = k*2;
}
C#
We can also use a loop to read the values of an array.
For example, we can display the contents of the array we just created:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int[ ] a = new int[10];
for (int k = 0; k < 10; k++) {
a[k] = k*2;
}
for (int k = 0; k < 10; k++) {
Console.WriteLine(a[k]);
}
}
}
}
OUTPUT
0
2
4
6
8
10
12
14
16
18
This will display the values of the elements of the array.
The variable k is used to access each array element.
The last index in the array is 9, so the for loop condition is k<10.
===================================================================
The foreach Loop
The foreach loop provides a shorter and easier way of accessing array elements.
The previous example of accessing the elements could be written using a foreach loop:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int[ ] a = new int[10];
for (int k = 0; k < 10; k++) {
a[k] = k*2;
}
foreach (int k in a) {
Console.WriteLine(k);
}
}
}
}
OUTPUT
0
2
4
6
8
10
12
14
16
18
The foreach loop iterates through the array a and assigns the value of the current element to the variable k at each iteration of the loop. So, at the first iteration, k=a[0], at the second, k=a[1], etc.
The data type of the variable in the foreach loop should match the type of the array elements.
Often the keyword var is used as the type of the variable, as in: foreach (var k in a). The compiler determines the appropriate type for var.
===================================================================
Arrays
The following code uses a foreach loop to calculate the sum of all the elements of an array:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int[ ] arr = {11, 35, 62, 555, 989};
int sum = 0;
foreach (int x in arr) {
sum += x;
}
Console.WriteLine(sum);
}
}
}
OUTPUT
1652
To review, we declared an array and a variable sum that will hold the sum of the elements.
Next, we utilized a foreach loop to iterate through each element of the array, adding the corresponding element's value to the sum variable.
The Array class provides some useful methods that will be discussed in the coming lessons.
=================================================================== Multidimensional Arrays
Multidimensional Arrays
An array can have multiple dimensions. A multidimensional array is declared as follows:
type[, , … ,] arrayName = new type[size1, size2, …, sizeN];
C#
For example, let's define a two-dimensional 3x4 integer array:
int[ , ] x = new int[3,4];
C#
Visualize this array as a table composed of 3 rows and 4 columns:
img-component
Array indexing starts from 0.
===================================================================
Multidimensional Arrays
We can initialize multidimensional arrays in the same way as single-dimensional arrays.
For example:
int[ , ] someNums = { {2, 3}, {5, 6}, {4, 6} };
C#
This will create an array with three rows and two columns. Nested curly brackets are used to define values for each row.
To access an element of the array, provide both indexes. For example someNums[2, 0] will return the value 4, as it accesses the first column of the third row.
Let's create a program that will display the values of the array in the form of a table.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int[ , ] someNums = { {2, 3}, {5, 6}, {4, 6} };
for (int k = 0; k < 3; k++) {
for (int j = 0; j < 2; j++) {
Console.Write(someNums[k, j]+" ");
}
Console.WriteLine();
}
}
}
}
OUTPUT
2 3
5 6
4 6
We have used two nested for loops, one to iterate through the rows and one through the columns.
The Console.WriteLine(); statement moves the output to a new line after one row is printed.
Arrays can have any number of dimensions, but keep in mind that arrays with more than three dimensions are harder to manage.
===================================================================
++=====================++
++=====================++ Jagged Arrays
Jagged Arrays
A jagged array is an array whose elements are arrays. So it is basically an array of arrays.
The following is a declaration of a single-dimensional array that has three elements, each of which is a single-dimensional array of integers:
int[ ][ ] jaggedArr = new int[3][ ];
C#
Each dimension is an array, so you can also initialize the array upon declaration like this:
int[ ][ ] jaggedArr = new int[ ][ ]
{
new int[ ] {1,8,2,7,9},
new int[ ] {2,4,6},
new int[ ] {33,42}
};
C#
You can access individual array elements as shown in the example below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int[ ][ ] jaggedArr = new int[ ][ ]
{
new int[ ] {1,8,2,7,9},
new int[ ] {2,4,6},
new int[ ] {33,42}
};
int x = jaggedArr[2][1];
Console.WriteLine(x);
}
}
}
OUTPUT
42
This accesses the second element of the third array.
A jagged array is an array-of-arrays, so an int[ ][ ] is an array of int[ ], each of which can be of different lengths and occupy their own block in memory.
A multidimensional array (int[,]) is a single block of memory (essentially a matrix). It always has the same amount of columns for every row.
===================================================================
++=====================++
++=====================++ Array Properties & Methods
Arrays Properties
The Array class in C# provides various properties and methods to work with arrays.
For example, the Length and Rank properties return the number of elements and the number of dimensions of the array, respectively. You can access them using the dot syntax, just like any class members:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int[ ] arr = {2, 4, 7};
Console.WriteLine(arr.Length);
Console.WriteLine(arr.Rank);
}
}
}
OUTPUT
3
1
The Length property can be useful in for loops where you need to specify the number of times the loop should run.
For example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int[ ] arr = {2, 4, 7};
for(int k=0; k<arr.Length; k++) {
Console.WriteLine(arr[k]);
}
}
}
}
OUTPUT
3
1
===================================================================
Array Methods
There are a number of methods available for arrays.
Max returns the largest value.
Min returns the smallest value.
Sum returns the sum of all elements.
For example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int[ ] arr = { 2, 4, 7, 1};
Console.WriteLine(arr.Max());
Console.WriteLine(arr.Min());
Console.WriteLine(arr.Sum());
}
}
}
OUTPUT
7
1
14
C# also provides a static Array class with additional methods. You will learn about those in the next module.
===================================================================
++=====================++
++=====================++ Working with Strings
Strings
It’s common to think of strings as arrays of characters. In reality, strings in C# are objects.
When you declare a string variable, you basically instantiate an object of type String.
String objects support a number of useful properties and methods:
Length returns the length of the string.
IndexOf(value) returns the index of the first occurrence of the value within the string.
Insert(index, value) inserts the value into the string starting from the specified index.
Remove(index) removes all characters in the string from the specified index.