How to use cast in C#

2 Answers

0 votes
using System;

class Program
{
    static void Main() {
        var arr = new byte[10];

        for (int i = 0; i < arr.Length; i++) {
            arr[i] = (byte)(i * 2); // Cast int to byte
        }
        for (int i = 0; i < arr.Length; i++) {
            Console.WriteLine(arr[i]);
        }
    }
}



/*
run:

0
2
4
6
8
10
12
14
16
18

*/

 



answered Nov 25, 2020 by avibootz
0 votes
using System;

class Program
{
    static void Main() {
        double d = 457.391;

        int n = (int)d; // Cast double to int
        
        System.Console.WriteLine(n);
    }
}



/*
run:

457

*/

 



answered Nov 25, 2020 by avibootz

Related questions

1 answer 106 views
106 views asked Jan 10, 2024 by avibootz
1 answer 130 views
130 views asked May 19, 2021 by avibootz
1 answer 175 views
2 answers 203 views
203 views asked Jan 12, 2017 by avibootz
2 answers 190 views
1 answer 163 views
...