Saturday, 13 April 2019

Class, Object, Arrays and Strings

Class and Object
A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods (member function which defines actions) into a single unit. In C#, classes support the polymorphism, inheritance and also provide the concept of derived classes and base classes.

In C#, Object is a real-world entity, for example, chair, car, pen, mobile, laptop etc. In other words, an object is an entity that has state and behaviour. Here, state means data and behaviour means functionality. An object is a runtime entity, it is created at runtime. An object is an instance of a class. All the members of the class can be accessed through the object. Let's see an example to create an object using the new keyword.
Eg:- using System;  
   public class Student  
    {  
        int id;//data member (also instance variable)    
        String name;//data member(also instance variable)    
         
    public static void Main(string[] args)  
        {  
            Student s1 = new Student();//creating an object of Student    
            s1.id = 101;  
            s1.name = "Suman Jaiswal";  
            Console.WriteLine(s1.id);  
            Console.WriteLine(s1.name);  
  
        }  
    } 

Arrays and Strings
Arrays are using for store similar data types grouping as a single unit. We can access Array elements by its numeric index. The array indexes start at zero. The default value of numeric array elements are set to zero, and reference elements are set to null.

Integer Array
Adding values to a C# array
Declaring and Initializing an Integer Array
int[] array = new int[4]; 
array[0] = 10;
array[1] = 20;
array[2] = 30;
array[3] = 40;

In the above code, we declare an Integer Array of four elements and assign the value to an array index. That means we assign values to array index 0 - 4. The following code shows how to add items in an Array using for 

int[] items = new int[100];
for (int cnt = 0; cnt <= 100; cnt++)
{
    items[cnt] = yourValueHere;
}

We can retrieve these values from the array by using a for a loop.
for (int i = 0; i < array.Length; i++)
{
  MessageBox.Show (array[i]);
}

String Array
Declaring and Initializing a String Array
string[] week = new string[7];
week[0] = "Sunday";
week[1] = "Monday";

The above C# code declares a string array of 7 strings and assigns some values to it.

string[] week = new string[] {"Sunday","Monday","Tuesday"};

The above code declares and initializes a string array with values
string str = week[1];

We can access the Arrays elements by providing its numerical index, the above statement we access the second value from the week Array.

In the following program, we declare an Array "week" capable of seven String values and assign the seven values as days in a week. Next step is to retrieve the elements of the Array using a for the loop. For finding the end of an Array we used the Length function of Array Object.

Eg:-string[] week = new string[7];
week[0] = "Sunday";
week[1] = "Monday";
week[2] = "Tuesday";
week[3] = "Wednsday";
week[4] = "Thursday";
week[5] = "friday";
week[6] = "Saturday";
for (int i = 0; i < = week.Length-1; i++)
{
    MessageBox.Show(week[i]);
}

How to resize an Array
An array can be resized with Array.Resize < T > Method , that means We make an array bigger or smaller. Array.Resize < T > Method Changes the number of elements of a one-dimensional array to the specified new size.

Array.Resize < T > - T is the type of the elements of the array.

This method should be used with only one dimensional Array. This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replace the old array with the new one.

Resize Array
Eg:-// Initialize array for example
char[] array = new char[5];
array[0] = 'A';
array[1] = 'B';
array[2] = 'C';
array[3] = 'D';
array[4] = 'E';
for (int i = 0; i < array.Length; i++)
{
    MessageBox.Show (array[i].ToString ());
}
Array.Resize(ref array, 3);
for (int i = 0; i < array.Length; i++)
{
    MessageBox.Show(array[i].ToString ());
}

Array.Resize(ref array, 3);
In the above code, we resize the array to 3 elements.

for..each loop and array
 Eg:- int[] array = { 10, 30, 50 }; //array declaration
foreach (int element in array)
{
  Console.WriteLine(element);
}

Converting String array to List
Eg:-string[] week = new string[7];
week[0] = "Sunday";
week[1] = "Monday";
week[2] = "Tuesday";
week[3] = "Wednsday";
week[4] = "Thursday";
week[5] = "friday";
week[6] = "Saturday";
List<string> lst = new List<string>(week);
foreach (string day in lst)
{
    MessageBox.Show(day);
}

How can I test if an array contains a certain value?
Eg:-string[] week = new string[7];
week[0] = "Sunday";
week[1] = "Monday";
week[2] = "Tuesday";
week[3] = "Wednsday";
week[4] = "Thursday";
week[5] = "friday";
week[6] = "Saturday";
string value = "Wednsday";
int pos = Array.IndexOf(week, value);
if (pos > -1)
    MessageBox.Show(value + " exist !");
else
    MessageBox.Show(value + " not exist !");

https://www.youtube.com/channel/UCKLRUr6U5OFeu7FLOpQ-FSw/videos

0 comments

Post a Comment