C #: Casting

Post Reply
User avatar
Jason
Site Admin
Posts: 749
Joined: Tue Jul 01, 2025 10:13 am

Code: Select all

// casting-in-c-charp.cs

Using System;

namespace CastingExamples{

	class CastingExample1
	{

	// When something is cast from something big to something small (shrunk), there is not automatic casting.

		static void Main(string[ ] args)
		{

		// Below is implicit casting from small to big memory.

		int integerGPA = 3;

		float floatGPA = integerGPA;

		Console.WriteLine(floatGPA);

		// Prints out "3.0"

		// Below is explicit casting from big to small memory

		float floatGPA = 3.0;

		int integerGPA = (int) floatGPA;

		Console.WriteLine(integerGPA);

		// Prints out "3"

		}
	}
}

 

POSTREACT(ions) SUMMARY

Post Reply