Menu Close

Blog

Slide 1

Microsoft Business Applications Blogposts, YouTube Videos and Podcasts

Helping Businesses with Technology

Slide 2

Microsoft Business Applications Blogposts, YouTube Videos and Podcasts

Helping Businesses with Technology

Slide 3

Microsoft Business Applications Blogposts, YouTube Videos and Podcasts

Helping Businesses with Technology

previous arrow
next arrow

HOW TO MAKE METHOD PARAMETERS OPTIONAL IN C#

OPTIONAL PARAMETERS

FOUR WAYS

1) Use parameter arrays
2) Method overloading
3) Specify parameter defaults
4) Use OptionalAttribute that is present in System.Runtime.InteropService namespace


1)Use parameter arrays


using System;
using System.Text;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            AddNumber(10, 20 new object[] {30,50,40});
        }

        public static void AddNumber(int firstNumber, int SecondNumber, params object[] restofNumbers)
        {
            int result = firstNumber + SecondNumber;
            if(restofNumbers != null)
            {
                foreach(int i in restofNumbers)
                {
                    result += i;
                }
            }
            Console.WriteLine(“Sum = ” + result);
            Console.ReadLine();
        }

    }

}


   

2) Making method parameters optional using Method overloading 


using System;
using System.Text;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            AddNumber(10, 20, new int[] {30,40});
        }
// over loading method
      public static void AddNumber(int firstNumber, int SecondNumber)
        {
            AddNumber(firstNumber, SecondNumber, null);
        }
       

        public static void AddNumber(int firstNumber, int SecondNumber, params int[] restofNumbers)
        {
            int result = firstNumber + SecondNumber;
            if(restofNumbers != null)
            {
                foreach(int i in restofNumbers)
                {
                    result += i;
                }
            }
            Console.WriteLine(“Sum = ” + result);
            Console.ReadLine();
        }

    }

}

3) Specify parameter defaults


using System;
using System.Text;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            AddNumber(10, 20, new int[] {30,40});
        }

        // optional parameters appears after all the parameters
     
        public static void AddNumber(int firstNumber, int SecondNumber,  int[] restofNumbers = null)
        {
            int result = firstNumber + SecondNumber;
            if(restofNumbers != null)
            {
                foreach(int i in restofNumbers)
                {
                    result += i;
                }
            }
            Console.WriteLine(“Sum = ” + result);
            Console.ReadLine();
        }

    }

}


   NAMED PARAMETERS

using System;
using System.Text;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            Test(1,c:20);
        }

        // optional parameters appears after all the parameters

        public static void Test(int a, int b = 10, int c = 20)
        {
            Console.WriteLine(“a = ” + a);
            Console.WriteLine(“b = ” + b);
            Console.WriteLine(“c = ” + c);
            Console.ReadLine();
        }

    }

}

 4)  Making Method parameters optional by using OptionalAttributes


using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            AddNumber(10, 20);
        }
        
        public static void AddNumber(int firstNumber, int SecondNumber,[Optional] int[] restofNumbers)
        {
            int result = firstNumber + SecondNumber;
            if (restofNumbers != null)
            {
                foreach (int i in restofNumbers)
                {
                    result += i;
                }
            }
            Console.WriteLine(“Sum = ” + result);
            Console.ReadLine();
        }
    }
}
   


Share this:

WHAT ARE PARTIAL CLASSES

WHAT ARE PARTIAL CLASSES

what are partial classes?
what are the advantages or uses of partial classes?
where are partail classes used?
Partial classes allows us to split class into 2 or more files. All these parts are then combined into a single class, when the application is compiled. the partial keyword can also be used to split a struct or an interface over two or more files.

Share this:

DIFFERENCE BETWEEN SYSTEM.STRING AND SYSTEM.TEXT.STRINGBUILDER

DIFFERENCE BETWEEN SYSTEM.STRING AND SYSTEM.TEXT.STRINGBUILDER

System.String is immutable  |||      StringBuilder is Mutable

As StringBuilder objects are mutable, they offer better performance than string objects of type
System.String, when heavy string manipulation is involved.
EXAMPLE OF SYSTEM.STRING  I.E STRING() METHOD
Stack       and       Heap
string                   c#
                            vidoe
                             tutorial
                             for
                             beginners


EXAMPLE  OF SYSTEM.TEXT.STRINGBUILDER
Stack       and       Heap
string                   c# video tutorial for beginners


using System;
using System.Text;
namespace ConsoleApplication4
{
    public class MainClass
    {
        public static void Main()
        {
            string userString = string.Empty;
            for(int i = 1; i <= 10000;i++ )
            {
                userString += i.ToString() + ” “;
                
            }
            Console.WriteLine(userString);
            Console.ReadLine();
            
            // using system.string  
            //string userstring = “C#”;
            //userstring += “Vidoe”;
            //userstring += “Tutorail”;
            //userstring += “for”;
            //userstring += “Beginners”;
            //Console.WriteLine(userString);
            //Console.ReadLine();
            //// using system.text.stringbuilder
            //StringBuilder userString = new StringBuilder(“C#”);
            //userString.Append(” Video”);
            //userString.Append(” Tutorial”);
            //userString.Append(” For”);
            //userString.Append(” Beginners”);
            //Console.WriteLine(userString.ToString());
            //Console.ReadLine();
        }
    }
}
   


Share this: