Sponsored Ad

Sunday, May 6, 2012

Find Out if a Given Number is Divisible by 3 in C#

To check if number is divisible by 3 or not you have to use the mode operator and if reminder is 0 it means it is divisible.

Find Out if a Given Number is Divisible by 3 in C#

using System;

namespace ConsoleHub
{
    class Programs
    {       
        static void Main(string[] args)
            {

                Console.WriteLine("Enter a Number:");
                int intDiv3 = Convert.ToInt16( Console.ReadLine());
                if (intDiv3 % 3 == 0)
                {
                    Console.WriteLine("The Number {0} is Divisible by 3", intDiv3);
                }
                else
                {
                    Console.WriteLine("The Number {0} is NOT Divisible by 3", intDiv3);
                }

                Console.ReadLine();
            }       
    }   
}

Return Object of a Class from a Function in C#

You can return a object of a class from a method. This is simple program which is returning the object of share class. You can see that the class variable value is also returning.

Return Object of a Class from a Function in C#

using System;

namespace ConsoleHub
{
    class Share
    {
        public int ShareNumber = 299;
    }
    class Programs
    {       
        static void Main(string[] args)
            {
                Share ObjS = ObjFactory(); 

                Console.WriteLine("The Return Object is: ");
               Console.WriteLine(ObjS.ToString());
            Console.WriteLine(ObjS.ShareNumber);

                Console.ReadLine();
            }

            static Share ObjFactory()
            {
                Share objShare = new Share();
                objShare.ShareNumber = 201;
                return objShare;
            }       
    }   
}

Return an Array from a Method in C#

This program is a example of returning an integer array from a method. Create an integer array in the function and return it. Remember that the function return type should be array of integer.

Return an Array from a Method in C#

using System;

namespace ConsoleHub
{
    class Programs
    {       
        static void Main(string[] args)
            {
                int[] ReturnArray = ArrayFactory();

                Console.WriteLine("The Return Array is: ");
                foreach (int d in ReturnArray)
                {
                    Console.Write(" {0}", d);
                }

                Console.ReadLine();
            }

            static int[] ArrayFactory()
            {
                int[] NumberQueue = { 5, 6, 3, 8, 9 };
                return NumberQueue;
            }       
    }
}

C# Program to Pass int into a Function by Reference

This program help you to pass int value by reference and when you pass a value you can modify it inside function only. As you can see the output is 5 more than the privious value.

C# Program to Pass int into a Function by Reference

using System;

namespace ConsoleHub
{
    class Programs
    {       
        static void Main(string[] args)
            {
                int Org_Value = 4;
                Console.WriteLine("Original Value: {0}", Org_Value);
                Add5(ref Org_Value);
                Console.WriteLine("Modified Value: {0}", Org_Value);

                Console.ReadLine();
            }

            static void Add5(ref int intValue)
            {
               intValue = intValue + 5;
            }       
    }
}

Example to Use Class Inside Another Class in C#

You can have a class inside another class (nested class). The example of nested class is given below. The program also tells that how to use each class variable and create object of each class.

The full syntax of nested class is written below.

Incase of any questions, comment below.

Example to Use Class Inside Another Class in C#

using System;

namespace ConsoleHub
{
    class Programs
    {
        string strPrograms = "Prgram class string.";
        class SubClass
        {
            string strSubClass = "SubClass class string.";

            static void Main(string[] args)
            {
                ConsoleHub.Programs objPrograms = new ConsoleHub.Programs();
                ConsoleHub.Programs.SubClass objSubClass = new ConsoleHub.Programs.SubClass();

                Console.WriteLine("The value of Programs Class Variable is: {0}", objPrograms.strPrograms);
                Console.WriteLine("The value of SubClass Class Variable is: {0}", objSubClass.strSubClass);

                Console.ReadLine();

            }
        }
    }
}

Write Two Nested Namespaces in C#

You can write the nested namespaces in C#. The nested namespaces provide more facility to organize your classes and code. This program is a example of nested namespace and creating an object of class and using the class level variable.

Write Two Nested Namespaces in C#

using System;

namespace ConsoleHub
{
    namespace Innner_NameSpace
    {
        class Programs
        {
            int ClassVar = 1;
            static void Main(string[] args)
            {
                ConsoleHub.Innner_NameSpace.Programs ObjPrograms = new ConsoleHub.Innner_NameSpace.Programs();
                ObjPrograms.ClassVar = 5;

                Console.WriteLine("The value of Class Variable is {0}", ObjPrograms.ClassVar);

                Console.ReadLine();

            }
        }
    }
}

How to Throw and Handle ArgumentException in C#

This program demonstrate the throwing a new exception of type ArgumentException. Just create a new exception and through it. to catch this type of specific exception specify in catch block.

How to Throw and Handle ArgumentException in C#

using System;

namespace ConsoleHub
{
    class Programs
    {
        static void Main(string[] args)
        {
            try
            {
                Print_String(null);
            }
            catch (System.ArgumentException ex)
            {
                Console.WriteLine("{0}", ex.Message);
            }
            Console.ReadLine();
        }

        private static void Print_String(string str)
        {
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }
            else
            {
            Console.WriteLine(str);
            }
        }
    }
}

Saturday, May 5, 2012

Char Array with foreach Loop in C#

This C# program is create a char array and then print the char array values using the foreach loop. The foreach loop start with first value of array and then iterate with all the values. The limitation of foreach that you can not update the array values.

Char Array with foreach Loop in C#

using System;

namespace ConsoleHub
{
    class Programs
    {
        static void Main(string[] args)
        {
            char[] CharArray = { 'm', 'n', 'o', 'p', 'q' };

            foreach (char ch in CharArray)
                Console.Write("->{0} ", ch);

            Console.ReadLine();
        }
    }
}

Initialize Char Array and Print in C#

The below program will help you to initialize the char array. While defining the array you can pass the set of value to directly initialize it. The for loop is used here to print the char array values.

Initialize Char Array and Print in C#

using System;

namespace ConsoleHub
{
    class Programs
    {
        static void Main(string[] args)
        {
            char[] CharArray = { 'A', 'B', 'C', 'D', 'E' };

            for (int i = 0; i < CharArray.Length; i++)
                Console.Write(" {0}", CharArray[i]);         

            Console.ReadLine();
        }
    }
}

Initialize a String Variable in C#

This program is showing different methods of initializing the given string variable.

Case 1 is about initializing the string variable with empty string

Case 2 is about initializing the string variable with null string

Case 3 is about initializing the string variable with a value string

image

using System;

namespace ConsoleLab
{
    class Programs
    {
        static void Main(string[] args)
        {
            //Initialize with empty string
            string EmptyString = "";
            //Initialize with null string
            string NullString = null;
            //Initialize with a value string
            string ValueString = "Sample";

            Console.WriteLine("Empty String: {0}", EmptyString);
            Console.WriteLine("Null String: {0}", NullString);
            Console.WriteLine("Value String: {0}", ValueString);

            Console.ReadLine();
        }
    }
}

Sponsored Ad

Development Updates