c# tutorial for beginners
Lección 69 de 102 · Part 69 Making method parameters optional by specifying parameter defaults · Bases de datos
Part 69 Making method parameters optional by specifying parameter defaults
Tags
c# 4.0 named and optional parameters
c# optional parameters named
c# named parameters example
c# method named parameters
c# function named parameters
named parameters c# example
c# method default parameters
c# optional parameters default value
optional parameters must specify a default value
c# named parameters method
C#, SQL Server, WCF, MVC and ASP .NET video tutorials for beginners
http://www.youtube.com/user/kudvenkat/playlists
In this video, we will discuss making method parameters optional by specifying parameter defaults.
This method allows us to add any number of integers.
public static void AddNumbers(int firstNumber, int secondNumber,
int[] restOfTheNumbers)
{
int result = firstNumber + secondNumber;
foreach (int i in restOfTheNumbers)
{
result += i;
}
Console.WriteLine(“Total = ” + result.ToString());
}
If we want to add 5 integers – 10, 20, 30, 40 and 50. We call the method as shown below.
AddNumbers(10, 20, new int[]{30, 40, 50});
At the moment all the 3 parameters are mandatory. If I want to add just 2 numbers, then I can invoke the method as shown below. Notice that, I am passing an empty integer array as the argument for the 3rd parameter.
AddNumbers(10, 20, new int[]{});
We can make the 3rd parameter optional by specify a default value of null for the 3rd parameter.
public static void AddNumbers(int firstNumber, int secondNumber,
int[] restOfTheNumbers = null)
{
int result = firstNumber + secondNumber;
// loop thru restOfTheNumbers only if it is not null
// otherwise you will get a null reference exception
if (restOfTheNumbers != null)
{
foreach (int i in restOfTheNumbers)
{
result += i;
}
}
Console.WriteLine(“Total = ” + result.ToString());
}
Since we have specified a default value for the 3rd parameter, it is optional. So, if we want to add just 2 numbers, we can use the function as shown below.
AddNumbers(10, 20);
Optional parameters must appear after all required parameters
The following method will not comiple. This is because, we are making parameter “a” optional, but it appears before the required parameters “b” and “c”.
public static void Test(int a = 10, int b, int c)
{
// Do something
}
The following method will compile, as optional parameter “a” is specified after all the required parameters (“b” & “c”).
public static void Test(int b, int c, int a = 10)
{
// Do something
}
Named Parameters
In the following method, parameters “b” & “c” are optional.
public static void Test(int a, int b = 10, int c = 20)
{
Console.WriteLine(“a = ” + a);
Console.WriteLine(“b = ” + b);
Console.WriteLine(“c = ” + c);
}
When we invoke this method as shown below, “1” is paased as the argument for parameter “a” and “2” is passed as the argument for parameter “b” by default.
Test(1, 2);
My intention is to pass “2” as the argument for parameter “c”. To achieve this we can make use of named parameters, as shown below. Notice that, I have specified the name of the parameter for which value “2” is being passed.
Test(1, c: 2);
- .Equals()
- .Net
- .net c# dynamically load assembly
- .net convert.tostring null
- .net convert.tostring vs tostring
- .net difference between struct and class
- .net early and late binding
- .net interface example
- .net interface vs abstract class
- .net nullable tostring
- .net override equals gethashcode
- .net partial class override method
- .net reflection get constructor
- .net reflection get method name
- .net thread complete callback
- .net tostring null
- #endregion directive expected c#
- #region c# syntax
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- a partial method cannot have access modifiers
- abstract
- abstract class vs interface in c#
- abuse
- Access
- Access Modifiers for types
- Access Modifiers in C#
- activator.createinstance c# example
- add item to stack c#
- add list of items to list c#
- add list of objects to list c#
- addrange
- advantage of using system.text.stringbuilder over system.string
- Advantages
- Advantages of arrays
- advantages of early binding and late binding
- an item with the same key has already been added. c# dictionary
- and
- anonymous
- anonymous method delegate c#
- anonymous method example in c#
- anonymous method predicate c#
- Arguments
- arithmetic
- arrays
- arrays in c++
- ascending
- asp net inner exception
- asp.net
- asp.net async and await
- asp.net enum example
- asp.net region c#
- aspx.cs and aspx.designer.cs
- AsReadOnly
- assembly metadata c#
- assembly metadata in .net
- assembly.getexecutingassembly() c#
- Assignment
- async
- Attributes in C#
- Auto Implemented Properties
- avoid
- await
- beginners
- between
- binding
- break
- built in types
- c sharp encapsulation example
- c sharp public private protected internal
- C#
- c# - delegates real time example
- c# 4.0 named and optional parameters
- c# access list element
- c# add item to queue
- c# anonymous function vs lambda expressions
- c# anonymous method arguments
- c# anonymous method lambda
- c# assembly gettype returns null
- c# async await task tutorial
- c# async await tutorial
- c# async await ui thread
- c# async await update ui
- c# attribute base class
- c# attribute error message
- c# attributes example
- c# auto implemented properties
- c# can a struct implement an interface
- c# check if inner exception is null
- c# check if item exists in queue
- c# class access modifiers
- c# class default access modifier
- c# class implements interface
- c# class inherit from struct
- c# class override tostring
- c# class property get set example
- c# class vs struct difference
- c# code snippets
- c# collapsible region
- c# compare two objects for equality
- c# comparison delegate
- c# complex object tostring
- c# constructorinfo example
- c# convert array to list
- c# convert list to array
- c# convert list to dictionary
- c# convert string null to empty
- c# convert.tostring null
- c# create a new thread
- c# create instance from type name
- c# create interface
- c# create multicast delegate
- c# create object from string config file
- c# create object from string type name
- c# create thread with parameters
- c# deadlock prevention
- c# default access modifier
- c# default access modifier for class
- c# default method parameters
- c# delegate access modifier
- c# delegate public
- c# delegate tutorial
- c# delegates basics
- c# delegates explained
- c# delegates for beginners
- c# delegates practical uses
- c# delegates tutorial for beginners
- c# diamond problem
- c# dictionary containskey example
- c# dictionary example foreach
- c# dictionary find by key
- c# dictionary performance vs list
- c# dynamically create object
- c# encapsulate field
- c# encapsulation example
- c# encapsulation explained
- c# endregion name
- c# enum example
- c# enum int example
- c# enum internal
- c# enum tutorial
- c# enumeration example
- c# expand collapse code
- c# find an object in a list
- c# find duplicates in list of objects
- c# func delegate multiple parameters
- c# func delegate with parameters
- c# function named parameters
- c# function overloading different return types
- c# function pointer delegate
- c# generic collection queue
- c# generic method example
- c# generic methods
- c# generic stack example
- c# generic vs non generic method
- c# get all class methods
- c# get all class properties
- c# get list item index
- c# get type of inner exception
- c# get value from stack
- c# implement icomparer interface
- c# implement two interfaces with same method names
- c# indexer advantages
- c# indexer declaration
- c# indexer example
- c# indexer operator
- c# indexer property
- c# indexer syntax
- c# indexer this
- c# indexers
- c# inner exception example
- c# inner exception null
- c# inner exception stack trace
- c# integer indexer
- c# interface access modifier
- c# interface class example
- c# interface implementation example
- c# internal class example
- c# last in first out collection
- c# lifo collection
- c# list add another list
- c# list add item
- c# list add multiple items at once
- c# list advantages
- c# list benefits
- c# list class example
- c# list class trueforall example
- c# list clear
- c# list contains method
- c# list find index of item
- c# list find lambda
- c# list find vs dictionary
- c# list findall predicate example
- c# list findindex predicate example
- c# list findlast example
- c# list findlastindex example
- c# list for loop
- c# list foreach example
- c# list get all elements
- c# list get element at position
- c# list getrange example
- c# list indexof method
- c# list insert at beginning
- c# list insert item at index
- c# list insert method
- c# list insertrange
- c# list or dictionary performance
- c# list remove range
- c# list removerange example
- c# list sort comparison delegate
- c# list sort comparison lambda
- c# list sort comparison t
- c# list sort failed to compare two elements
- c# list t trimexcess
- c# list t trueforall
- c# lock thread safe
- c# method default parameters
- c# method named parameters
- c# method overloading example
- c# method overloading return type
- c# method overloading vs optional parameters
- c# method parameter default value
- c# method params keyword
- c# methodinfo example
- c# monitor example
- c# monitor.tryenter
- c# multicast delegate example
- c# multiple class inheritance
- c# multiple inheritance
- c# multiple inheritance workaround
- c# multiple interface inheritance
- c# multiple methods same name
- c# multiple threads performance
- c# multithreading lock example
- c# mutex class
- c# mutex example
- c# named parameters example
- c# named parameters method
- c# object.equals vs ==
- c# obsolete attribute example
- c# obsolete constructor
- c# obsolete error
- c# obsolete message
- c# obsolete parameter
- c# obsolete warning
- c# oop encapsulation
- c# optional int parameters
- c# optional parameter null
- c# optional parameter nullable
- c# optional parameters default value
- c# optional parameters named
- c# optionalattribute default value
- c# optionalattribute example
- c# parameter array
- c# parameter int null
- c# parameter nullable
- c# params null
- c# partial abstract method
- c# partial class access members
- c# partial class fields
- c# partial class interface implementation
- c# partial class member variables
- c# partial class multiple inheritance
- c# partial class properties
- c# partial method multiple implementations
- c# predefined attributes
- c# print dictionary contents
- c# process vs thread
- c# properties advantages
- c# properties example
- c# properties explained
- c# properties get set example
- c# properties tutorial
- c# propertyinfo example
- c# public partial class inheritance
- c# public vs private vs protected vs internal
- c# queue contains
- c# queue dequeue
- c# queue enqueue
- c# queue foreach
- c# queue get all items
- c# queue peek example
- c# queue peek vs dequeue
- c# read write property
- c# readonly property example
- c# reflection create instance from dll
- c# reflection get constructor
- c# reflection get method name
- c# reflection get property name
- c# region example
- c# remove all elements from list
- c# remove from queue
- c# return readonly list
- c# split class into multiple files
- c# stack contains
- c# stack data structure
- c# stack foreach
- c# stack peek example
- c# stack pop example
- c# stack push pop example
- c# string concatenation best practice
- c# string indexer example
- c# string indexer property
- c# stringbuilder vs string concatenation
- c# struct access modifiers
- c# struct and class difference
- c# struct class heap stack
- c# struct constructor
- c# struct constructor parameters
- c# struct default access modifier
- c# struct destructor
- c# struct example
- c# struct inherit from class
- c# struct internal
- c# struct methods
- c# struct object initializer
- c# struct parameterless constructor
- c# struct public
- c# struct stack heap
- c# system.attribute class
- c# task blocks ui thread
- c# task example await
- c# task update ui
- c# thread blocking ui
- c# thread call method with parameters
- c# thread callback delegate
- c# thread class example
- c# thread execute method
- c# thread lock example
- c# thread lock tutorial
- c# thread monitor example
- c# thread parameter start
- c# thread parameters
- c# thread pass parameter
- c# thread send data
- c# thread sleep example
- c# thread start method
- c# thread with parameters example
- c# thread.join blocked
- c# threading performance
- c# threads performance
- c# todictionary linq example
- c# tostring null empty
- c# tostring null object
- c# tostring null value
- c# tostring nullreferenceexception
- c# tostring object reference not set to an instance of an object
- c# type access modifiers
- c# type class example
- c# type getconstructor example
- c# type.getmethods
- c# ui thread blocking
- c# use object as array
- c# using optionalattribute
- c# wait for asynctask to finish
- c# wait for task to finish before continuing
- c# wait without blocking ui
- c# what is thread
- c# winforms async await
- c#.net
- c#.net generics example
- c#.net struct example
- c++ tutorial
- c++ tutorial for beginners
- Callback
- can a class inherit multiple interfaces in c#
- can you create an instance of an interface in c#
- check if 2 objects are equal c#
- check if two objects are the same c#
- class
- class attributes c#
- classes
- code
- code snippet manager visual studio
- collection
- Collections
- comments in c#
- compare 2 object c#
- compare two object values c#
- comparison
- complex types
- Concepts.
- concurrent
- Conditional
- constructors
- Contains
- continue
- continued
- convert non generic method to generic method c#
- convert null to empty string c#
- convert stringbuilder object to string
- Convert.ToString()
- core
- create dictionary c#
- create func delegate
- Creating
- creating indexers
- csharp
- csharp tutorial
- custom
- Data
- deadlock
- deadlock solution c#
- Delegate
- delegate default access modifier
- delegate types in .net
- Delegates
- delegates c# programming guide
- dequeue
- descending
- Destructors
- diamond problem in oops
- dictionary
- dictionary get value by key
- dictionary tkey tvalue c# example
- dictionary trygetvalue
- difference
- difference between delegate and multicast delegate
- difference between early binding and late binding
- difference between thread and process in c#
- dis-advantages of arrays
- disadvantages
- Do
- does c# support multiple inheritance
- dot net
- dot net interface example
- dot net prevent exception handling abuse
- dotnet
- early binding and late binding in c#
- Elements defined in a namespace cannot be explicitly declared as private
- Encapsulation
- encapsulation example c# net
- enqueue
- entrypoint
- enum in c#.net with example
- Enums
- equals vs == c#
- error inaccessible due to its protection level
- Escape Sequence
- example
- example of getter and setter methods in c#
- exception
- Exception handling abuse example
- exception handling abuse in c#
- exceptions
- Exists
- Explicit
- Explicit Conversions
- expression
- fifo collection c#
- FIND
- find value in dictionary by key c#
- FindAll
- FindLast. FindIndex
- FindLastIndex
- first in first out collection c#
- for
- foreach
- Func
- func c# example
- func c# tutorial
- func delegate with parameters
- func t tresult delegate example
- func vs lambda expression
- function
- function delegate parameter
- function overloading in c#
- function overloading in c#.net with example
- functions
- generic
- generic collections
- Generics
- generics in C#
- get inner exception message
- GetRange
- getter and setter c# example
- handling
- Hiding
- How are parameters passed in C#
- how to add partial class in visual studio
- how to check if key exists in dictionary c#
- how to pass optional parameter in c# method
- how to see inner exception visual studio
- how to use enum in dotnet
- icomparable and icomparer interface in net
- icomparable compareto c# example
- if
- if two interfaces have same method in c#
- Implementation
- Implicit conversions
- in
- in C# multithreading
- inaccessible due to protection level c#
- increment
- indexer in c sharp
- indexer overloading
- indexers in c#
- indexers usage
- Inheritance
- inner
- insert a list into a list c#
- insert into dictionary c#
- InsertRange
- instance
- interface
- interface fields c#
- interface inherit from interface
- interfaces
- Interlocked
- Interlocked.Increment
- internal access modifier
- introduction to c#
- isalive
- iterate over dictionary c#
- Join
- keyword
- lambda expression in c# tutorial
- lambda expression tutorial
- lambda vs anonymous function
- lambda vs anonymous function c#
- lambda vs anonymous methods
- lamdba
- late
- list
- list addrange method c#
- lock
- locking
- loop
- loop through dictionary object c#
- loop through key value pair c#
- loop thru dictionary c#
- members
- method
- method overloading
- method overloading c# codeproject
- method overloading c# msdn
- method overloading signature c#
- methodinfo invoke parameters
- methodinfo.invoke example in c#
- methodinfo.invoke return value
- methods
- missing partial modifier on declaration of type in c#
- monitor
- monitor vs lock
- msdn c# system.type.gettype
- multi
- multi-threading
- Multicast
- multicast delegates in c#.net with example
- multiple
- multiple inheritance diamond problem c#
- multiple inheritance example in c#.net
- multiple inheritance using interface in c# example
- multiple threads on multiple cores
- multiple threads on single core
- multithreaded
- multithreaded programming
- Multithreading
- multithreading context switching
- multithreading deadlock example
- multithreading issues
- multithreading on multicore processors
- multithreading on multiprocessor
- multithreading on single processor
- multithreading problems and solutions
- multithreading shared memory
- multithreading single core vs multi core
- named parameters
- named parameters c# example
- Namespaces
- need for properties
- no defining declaration found for implementing declaration
- Null Coalesce Operator
- nullable types
- number of processors computer
- object equality c#
- obsolete c# bool
- of
- operators
- Optional
- optional attribute
- optional parameters must specify a default value
- or protected internal
- out
- overloading
- overloading in c#.net with example
- overloading indexers
- override
- override equals and gethashcode c#
- override tostring example
- Overriding
- Parameter
- parameter defaults
- ParameterizedThreadStart
- parameterizedthreadstart example in c#
- Parameters
- Parse and TryParse difference
- Part
- part 1
- part 2
- Part 26 C# Tutorial
- part 3
- part 4
- part 5
- part 6
- part 8
- part 9
- Part1
- partial
- partial abstract class c#
- partial class inherit from interface
- partial class inheritance c#
- partial class virtual method
- partial declarations conflicting accessibility modifiers
- partial declarations must not specify different base
- Partial method
- partial methods must have empty bodies
- partial sealed class c#
- Passing
- peek
- performance
- performance threading .net
- polymorphism
- Pop
- practical example of delegate in c#
- preventing
- preventing exception handling abuse example
- preventing exception handling abuse in c#
- private
- private vs public c#
- problems
- problems of multithreading
- problems with public fields
- processor
- program
- programming
- properties encapsulate access to private fields
- protected
- protected internal access modifier
- protected vs private c#
- protecting
- Public and Protected
- public delegate void c#
- public override string tostring() c#
- public private protected access specifier
- public private protected example
- public private protected net
- public vs protected vs private
- push
- queue
- ranges
- real world example of delegates in c#
- Real-time
- real-world
- realtime
- reasons
- Reference
- reference equality in c#
- Reflection
- reflection find method c#
- reflection find property by name
- reflection list methods c#
- reflection method return type
- remove all items from dictionary c#
- remove item from c# dictionary
- RemoveRange
- resolve
- resolve deadlock c#
- resources
- retrieving
- reverse
- safe
- saftety
- shared
- similarities and difference between class and structure in .net
- simple delegate example c#
- simple lambda expression example c#
- single
- snippets
- sort
- Sorting
- stack
- stack push method c#
- statement
- static
- String
- string stringbuilder performance
- string type
- StringBuilder
- stringbuilder append function
- stringbuilder append method c#
- stringbuilder namespace in c#
- stringbuilder vs string concatenation c# performance
- structs
- surround with code snippet
- switch
- synchronization
- system.comparison example c#
- system.comparison t
- System.Runtime.InteropServices
- system.text.stringbuilder performance
- task
- task async and await in c#
- ternary
- Thread
- thread callback function c#
- thread callback method c#
- thread deadlock example
- thread join blocking
- thread parameters
- thread values
- Thread.IsAlive
- Thread.Join
- thread.join blocks ui
- threading deadlock example
- threading shared memory
- threadstart
- threadstart vs parameterizedthreadstart
- to
- tostring vs convert.tostring in c#
- ToString()
- TrimExcess
- TrueForAll
- tutorial
- two interfaces having same method c#
- type
- type members in c#
- type safety
- Types
- ui unresponsive c#
- usage
- use case for stack
- use of delegates in c#
- use of indexer in c#.net
- uses
- using
- using linq with dictionary c#
- V.S.
- value
- value equality c#
- verbatim literal
- versus
- Visual Studio
- visual studio add partial class
- visual studio code regions
- visual studio code snippet
- visual studio indexer
- visual studio inner exception
- visual studio partial class
- visual studio web form partial class
- wait for worker thread to finish c#
- what are indexers in c#
- what are types in c#
- what is deadlock in computer science
- what is indexer
- what is meant by deadlock
- what is multicast delegate explain with example
- what is process in c#
- what is the difference between == and equals method in c#
- what is the use of properties in c#
- when to use stack data structure
- when to use stringbuilder over string
- when to use structure and class in c#
- While
- Why
- why do we need generics in c#
- why public fields are bad
- why we use indexers in c#
- why we use method overloading in c#
- windows form unresponsive
- winforms async await example
- winforms async button click
- winforms async event handler
- winforms block ui
- working with enums c#
- write only property in c#.net