Some Important example of Methods in C#( C -Sharp)


In this article I explain about some useful and important methods in C Sharp. With the help of given example, we learn how to use methods in c Sharp.

Some Important example of Methods in C#(C-Sharp)



Adding a method is the same as adding any other fuction . In fact a function and method are essentially the same in the .NET world . If you do not want the function to be exposed outside just make sure it access modifier is set to protected or private. This article , I posted Some Important example of Methods in C#(Sharp):

CopyTo():


CopyTo takes string characters and puts them into an array. This method helps to copies componants of the ICollection object to any array, starting at a particular Array index. If .NET is disable to cast source type to destination, then it throws ArrayTypeMismatchException exception.
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace CollectionexampleTestDemo
{
class Program
{
static void Main(string[] args)
{
ArrayList srcList = new ArrayList();
srcList.Add("Milk");
srcList.Add("water");
srcList.Add("Caca");
srcList.Add("Votka");

string[] destList = new string[srcList.Count];
srcList.CopyTo(destList, 0);
Console.WriteLine("This is your Destination list ExampleItems:");
foreach (var lst in destList)
{
Console.WriteLine("{0}", lst.ToString());
}
Console.ReadKey();
}
}
}

Add ():


Add (object value): Add places an element at the end of a List. It adds the item into the IList.

ArrayList arryList = new ArrayList();
arryList.Add("Milk");
arryList.Add("Water");
arryList.Add("Coca");
arryList.Add("Votka");

Insert ():


Insert (int index, object value):
It inserts an item to the IList at specific index. Insert places one string into another. This forms a new string in your C# program. If index equals the number of ExampleItems in the IList, then value is appended to the end, but if index greater than the number of ExampleItems in the IList or less than zero, then it throws ArgumentOutOfRangeException exception.
     ArrayList arryList = new ArrayList();
arryList.Add(new Testing(1, "ExampleExampleItems1"));
arryList.Add(new Testing(2, "ExampleItems2"));
arryList.Add(new Testing(3, "ExampleItems3"));
Testing tst = new Testing(4, "ExampleItems4");
arryList.Insert(3, tst);

Remove ():


Remove (object value):
It removes the first occurrence of a specific object from the IList.Remove eliminates a range of characters. The Remove method on the string class is useful for shortening strings If you try to remove value from read only or fixed size IList, then it throws NotSupportedException.

ArrayList arryList = new ArrayList();
arryList.Add(new Testing(1, "ExampleItems1"));
arryList.Add(new Testing(2, "ExampleItems2"));
arryList.Add(new Testing(3, "ExampleItems3"));
Testing tst = new Testing(4, "ExampleItems4");
arryList.Remove(tst);

Contains ():


Contain (object value):
Contains searches strings. It checks if one substring is contained in another. It returns true if IList contain a specific value. This method uses the Equals and CompareTo methods to determine whether an item exists in Collection.

using System;
class Program
{
static void Main()
{
Test("Dot Net Spiders");
Test("dot net spiders");
}
static void Test(string input)
{ Console.Write("--- ");
Console.Write(input);
Console.WriteLine(" ---");
bool contains = input.Contains("Net");
Console.Write("Contains 'Net': ");
Console.WriteLine(contains);
if (input.Contains("Spiders"))
{
Console.WriteLine("Contains 'Spiders'");
}
if (!input.Contains("Dot"))
{
Console.WriteLine("Doesn't Contain 'Dot'");
}
}
}

Clear ():


Array.Clear zeros out all elements. It provides a one-line, reliable and understandable way to empty or clear your array. It removes all elements from the IDictionary object.

Hashtable hashList = new Hashtable();
hashList.Add(1, "Pakistan ");
hashList.Add(2, "India");
hashList.Add(3, "United State America");
hashList.Clear();


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: