0% found this document useful (0 votes)
24 views13 pages

Params Array

Uploaded by

thirosul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views13 pages

Params Array

Uploaded by

thirosul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Params Array

Params is an important keyword in C#. It is used as a parameter which can take


the variable number of arguments.
Important Point About Params Keyword :
 It is useful when programmer don’t have any prior knowledge about the
number of parameters to be used.
 Only one Params keyword is allowed and no additional Params will be
allowed in function declaration after a params keyword.
 The length of params will be zero if no arguments will be passed.

 Syntax
 MethodName ( params type[ ] variableName ) { }

 C# Parameter array is declared with the params modifier.


 Parameter type must be declared as an array.
 Must be of a single-dimensional Array type.
 Single params array per method is allowed.
 Must be the last argument in the method’s parameter list.

MethodName ( int a, int b, params int[ ] c )

MethodName ( params int[ ] a, int b )

1. using System;
2. namespace AccessSpecifiers
3. {
4. class Program
5. {
6. // User defined function
7. public void Show(params int[] val) // Params Paramater
8. {
9. for (int i=0; i<val.Length; i++)
10. {
11. Console.WriteLine(val[i]);
12. }
13. }
14. // Main function, execution entry point of the program
15. static void Main(string[] args)
16. {
17. Program program = new Program(); // Creating Object
18. program.Show(2,4,6,8,10,12,14); // Passing arguments of variable length
19. }
20. }
21.}

Output:

2
4
6
8
10
12
14

Tuples In C#

C# tuple is a data structure that provides an easy way to represent a single


set of data. The System.Tuple class provides static methods to create tuple
objects.

A tuple in C# allows us to store elements of different data types.

var student = ("Taylor", 27, "Orlando");


 It allows us to represent multiple data into a single data set.
 It allows us to create, manipulate, and access data set.
 It return multiple values from a method without using out parameter.
 It can also store duplicate elements.
 It allows us to pass multiple values to a method with the help of single
parameters.

Create Tuple in C#

C# provides various ways for creating tuples. We will learn about the following
methods:

1. Using Parentheses

2. Using the Create() Function

1. C# Tuple Using Parentheses

We can create a tuple by directly assigning different values using parentheses ().
For example,

using System;

class Program {
public static void Main() {
// create a tuple containing 3 elements
var student= ("Taylor", 5, "Orlando");

Console.WriteLine(student);
}
}

Output

("Taylor", 5, "Orlando")

In the above example, we have created a tuple without mentioning the datatypes of
the elements.
In this way, we can store data elements of different data types inside a tuple
without mentioning the datatype.
using System;

using System;

class Program {
public static void Main() {

(string,int,string) student = ("Taylor", 5, "Orlando");

Console.WriteLine(student);
}
}
// Output: (Taylor, 5, Orlando)

2. C# Tuple using Create() Method

In C# we can also use the Create() method to create a tuple without having to
mention the datatypes of the tuple elements.
The syntax for creating tuple using Create() is:

var t1 = Tuple.Create(value);

To understand it clearly, let's see an example:

using System;

class Program {
public static void Main() {
// create a tuple containing 3 elements
var programming = Tuple.Create("programiz", "java", 12);
Console.WriteLine(programming);
}
}

Output

(programiz, java, 12)

Access Tuple Elements

In the previous example, we have directly displayed the whole tuple. However, it is
possible to access each element of the tuple.

In C# each element of the tuple is associated with a default name.

 first element - Item1


 second element - Item2
 and so on

We can access elements of tuple using the default name. For example,

using System;

class Program {
public static void Main() {

var subjects = Tuple.Create("Science", "Maths", "English");


// access the first element
Console.WriteLine("The first element is " + subjects.Item1);

// access the second element


Console.WriteLine("The second element is " +subjects.Item2);
}
}

Output
The first element is Science
The second element is Maths

In the above example:

 subjects.Item1 - accesses the first element


 subjects.Item2 - accesses the second element

Change Value of the Tuple Element

We can change the value of data inside a tuple. To change the elements of the
tuple, we can reassign a new value to the default name. For example,

using System;

class Program {
public static void Main() {
var roll_num = (5, 7, 8, 3);

// original tuple
Console.WriteLine("Original Tuple: " + roll_num);

// replacing the value 7 with 6


roll_num.Item2 = 6;
Console.WriteLine("Changing value of 2nd element to " + roll_num.Item2);
Console.WriteLine("Modified Tuple: " + roll_num);
}
}

Output

Original Tuple: (5, 7, 8, 3)


Changing value of 2nd element to 6
Modified Tuple: (5, 6, 8, 3)

In the above example, we have replaced the value of the 2nd element with 6.
Note: If we have used Create() to create a tuple, then we cannot change the value
of the elements. That means the elements of the tuple are read-only.
For example,

using System;

class Program {
public static void Main() {
var t1= Tuple.Create("Taylor", "Jack");
t1.Item2 = "Monica";

Console.WriteLine(t1.Item2);
}
}

Output

Property or indexer 'Tuple<string, string>.Item2' cannot be assigned to -- it is read


only

Nested Tuple

We can create a tuple inside another tuple. A tuple within a tuple is called a nested
tuple. For example,

using System;

class Program {
public static void Main() {

var myTuple= Tuple.Create("Taylor", "Jack", Tuple.Create(7, 8, 9));

Console.WriteLine("The elements inside myTuple: " + myTuple);


}
}

Output

The elements inside myTuple: (Taylor, Jack, (7, 8, 9))

Here, we have a tuple

C# Ternary Operator (?:) elvis operator


1. In c#, Ternary Operator (?:) is a decision-making operator
2. it is a substitute for the if…else statement in c# programming language.

3. Using Ternary Operator, we can replace multiple lines of if…else


statement code into a single line in c# programming language.

4. The Ternary operator will help you execute the statements based on the
defined conditions using the decision-making operator (?:).

Syntax of C# Ternary Operator


condition_expression? first_expression : second_expression;

In c#, the Ternary Operator (?:) will work as follow.


 In Ternary Operator, the condition expression must be evaluated to be
either true or false. If the condition is true, the first_expression result is
returned by the ternary operator.
 In case the condition is false, then the second_expression result is
returned by the operator.

using System;

namespace Tutlane
{
class Program
{
static void Main(string[] args)
{
int x = 5, y = 20;
string result;
//Ternary Operator (?:)
result = (x > y) ? "x value greater than y" : "x value less than y";
Console.WriteLine(result);

Console.ReadLine(); } }

output :- x value less than y

C# Nullable

 In C#, Nullable is a concept that allows a type to hold an additional


value null.
 In other words, we can make a variable Nullable, so, it can hold additional
null value.
 All Nullable variables are the instances of System.Nullable<T> struct.
 The concept of Nullable is useful when we deal with databases that contain
elements that may not be assigned a value.
 we cannot create Nullable of reference type variable.

C# provides two different ways to create Nullable types.

1. By creating System.Nullable instance,


2. By using ? operator

1. using System;
2. namespace CSharpFeatures
3. {
4. class NullableExample2
5. {
6. static void Main(string[] args)
7. {
8. Nullable<int> a = 10;
9. Nullable<double> d = 10.10;
10. Nullable<char> c = 'S';
11. Nullable<bool> b = false;
12. // Displaying value
13. Console.WriteLine(a.Value);
14. // assigning null values
15. a = null;
16. d = null;
17. c = null;
18. b = null;
19. // Checking, does "a" contain value ?
20. if (a.HasValue)
21. {
22. Console.WriteLine(a.Value);
23. }
24. if(a == null)
25. Console.WriteLine("It contains null value");
26. }
27. }
28.}

Output:

10
It contains null value

1. using System;
2. namespace CSharpFeatures
3. {
4. class NullableExample
5. {
6. static void Main(string[] args)
7. {
8. // Integer nullable
9. int? a = 10;
10. // Float nullable
11. double? f = 10.10;
12. // Boolean nullable
13. bool? b = false;
14. // Char nullable
15. char? c = 'S';
16. // Checking value is present or not
17. if (a.HasValue)
18. {
19. Console.WriteLine(a.Value);
20. }
21. else Console.WriteLine("a contains null value");
22. // Assigning null value
23. a = null;
24. if (a.HasValue) // Checking again
25. {
26. Console.WriteLine(a.Value);
27. }
28. else Console.WriteLine("a contains null value");
29. }
30. }
31.}

Output:

10
a contains null value

Null Coalescing (??) Operator in C#

The ?? operator is also known as the null-coalescing operator.

 It returns the left side operand if the operand is not null else it returns the right
side operand.

The ?? operator is used to check null values and you can also assign a default
value to a variable whose value is null(or nullable type).
 You are not allowed to overload ?? operator.
 It is right-associative.
 In ?? operator, you can use throw expression as a right-hand operand of ??
operator which makes your code more concise.
 You are allowed to use ?? operator with value types and reference types.

using System;
namespace Demo {

class Program {

static void Main(string[] args) {


double? num1 = null;
double? num2 = 6.32123;
double num3;

num3 = num1 ?? 9.77;


Console.WriteLine(" Value of num3: {0}", num3);

num3 = num2 ?? 9.77;


Console.WriteLine(" Value of num3: {0}", num3);
Console.ReadLine();
}
}
}

Value of num3: 9.77


Value of num3: 6.32123

You might also like