Monday, May 25, 2020

C# Tutorial: 10 - Classes in C#

Tutorial 10: Classes & Objects

As you know C# is an Object Oriented Programming Language, so things are associated with classes and objects with attributes and methods included. For example a Keyboard's attributes include Type, Color etc and methods include KeyPress, KeyUp, KeyDown etc. When you define a class you are actually defining what your data will be and what type of methods you can perform on that specific data type. Objects are instances of a class. When a class is declared, there is no memory assigned to objects. Memory is assigned as soon as object of a class is created.

Declaring Classes

A class is declared using class keyword in C#. The general syntax for creating a class can be as follow:
//Access modifier - class - class identifier
public class Vehicle
{
// Properties,
Fields, Methods and Events
}
where Vehicle is class name.

Adding fields to a Class

Variables which are declared and used in a class are referred as fields. These fields can be of any 
data types available in C#. If you specify no access modifier before field it will be internal.
We will discuss access modifier in next tutorial. Below is an example how can you add fields
to a class.
public class Vehicle
{    
    int no_of_wheels;
    string color;
    int engine_no;
}
In above example, there are three fields, two of integer type and one of string type.

Adding methods to a Class

You can also add methods to a class. Methods in class defines how an object/instance of class will behave.
public class Vehicle
{    
    int no_of_wheels;
    string color;
    int engine_no;
    public void VehicleInfo()
    {
        Console.WriteLine("Color of Vehicle is"+color);
        Console.WriteLine("No of Wheels, in vehicle are "+no_of_wheels);
        Console.WriteLine("Engine No. of Vehicle is"+engine_no);
    }
}

Creating an Object

A class and objects are two different things. A Class is actually defining an object whereas an object is an instance of a class. When you create an instance reference of class is returned to the used for example in example below v1 holds the reference of Vehicle instance. You can create an object by using new keyword:
Vehicle v1 = new Vehicle();

Accessing Class Members

You can access members of a class using . (dot) operator.
Vehicle v1 = new Vehicle();
Console.WriteLin(v1.color);
v1.VehicleInfo();

Here is complete example of above code.

Output of above code will be:
Vehicle Color is: Blue
Engine No is: 1234
No. of wheels in Vehicle are: 4
In next tutorial we will discuss access modifiers.

Wednesday, May 20, 2020

C# Tutorial: 09 - String Handling in C#

Tutorial: 09 - String Handling in C#

    In strings, text is stored as read only and in sequential form of characters. In C#, string and String are equivalent. There are various ways by using which you can create a string in C# as shown in snippet below:

String Methods
  • Compare  
This method is used for comparing two strings. If both strings are equal it will return 0 and if both strings are not equal it will return -1. See following snippet
string str1 = "Hello";
string str2 = "Hellow";
if (String.Compare(str1, str2) == 0)
{

    Console.WriteLine("Both are equal.");
}
else
{
    Console.WriteLine("Both are not equal.");
}
  • Contains
This method is used for checking whether a string or word exists in another string or not. It returns boolean true or false based on evaluation. See following snippet
string s = "This is a cat";         
if (s.Contains("cat"))
{

    Console.WriteLine("Cat Found.");
}
  • Substring
Substring is used for creating another string from existing string. It takes one parameter which specifies the index from where you want to break the string in parts. Additionally you can specify second parameter which will be length. See following snippet
string s1 = "Visual Programming";
        Console.WriteLine(s1.Substring(7));
// Output: Programming

string s2 = "Visual Programming";
        Console.WriteLine(s2.Substring(7, 7));
// Output:
Program
  • IndexOf
IndexOf is used for locating occurrence of a character or word in a string. When found it returns the index of value. If there are more than one occurrences of a word or character, it will return the index of first occurrence.  See following snippet
string s = "This is a cat";
Console.WriteLine(s.indexOf("cat"));
//Output: 10
Following are most common method used in handling of strings.
  • Equals(string value)
  • Insert(int startIndex, string value)
  • IsNullOrEmpty(string value)
  • Remove(int startIndex)
  • Replace(string oldValue, string newValue)
  • StartsWith(string value)
  • EndsWith(string value)
  • ToLower()
  • ToUpper()
  • Torim()
There are many more methods which we can't cover in a single topic. 
For more detailed information you can visit official link of microsoft.
 

Tuesday, May 19, 2020

C# Tutorial: 08 - Methods in C#

Tutorial: 08 - Methods in C#

Method is a block of code which have set of statements which performs some task(s). A method is executed with it is called by using its name and providing any required information by using parameters. Methods provides re-usability to code as you can call a method again and again as per your requirements.

Defining a Method
Here is a general syntax of a method in C#.
<Access Specifier> <Return Type> <Method Name>(Parameters) 
{
Statement(s)
}

Access Specifier are used to control access level or visibility of methods.
Return Type specifies what type of value will be returned by the method.
Method Name is an identifier which is used for identifying the method uniquely.
Parameters are used to provide additional data required by method while its execution.
Statement part contains actual task which will be performed by the method.
Lets see an example of a method which will take a number a parameter and will check whether
a number is even or odd.


Passing Parameters
You can pass additional information to a method by using parameters. You can pass as many
as required parameters to a method. Parameters can be of different data types or they can be of
same data type. See following example for Parameter Passing in which we are passing 3 different
parameters to a method named PrintNumbers.

Return Values
A method can return value after executing its statements. Return keyword is used in order to
return value from method. If you want nothing to be return, use void. Example for return values
are given below:


We will discuss about Access Specifiers with classes then we will also explore various examples
for controlling visibility of methods.