Params Array
Params Array
Syntax
MethodName ( params type[ ] variableName ) { }
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#
Create Tuple in C#
C# provides various ways for creating tuples. We will learn about the following
methods:
1. 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() {
Console.WriteLine(student);
}
}
// Output: (Taylor, 5, Orlando)
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);
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
In the previous example, we have directly displayed the whole tuple. However, it is
possible to access each element of the tuple.
We can access elements of tuple using the default name. For example,
using System;
class Program {
public static void Main() {
Output
The first element is Science
The second element is Maths
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);
Output
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
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() {
Output
4. The Ternary operator will help you execute the statements based on the
defined conditions using the decision-making 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(); } }
C# Nullable
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
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 {