Escolar Documentos
Profissional Documentos
Cultura Documentos
NET
Try Following Programs in Visual Studio using C#. Following material is given to you for practice only.
1.
2.
3.
4.
// Demonstrating Simple Program using System; namespace Test { class Program { static void Main(string[] args) { int myInteger; string myString; myInteger = 17; myString = "\"myInteger\" is"; Console.WriteLine("{0} {1}.", myString, myInteger); Console.ReadKey(); } } } // Demonstrating Type Conversions using System; namespace Test { class Program { static void Main(string[] args) { double firstNumber, secondNumber; string userName; Console.WriteLine("Enter your name:"); userName = Console.ReadLine(); Console.WriteLine("Welcome {0}!", userName); Console.WriteLine("Now give me a number:"); firstNumber = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Now give me another number:"); secondNumber = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("The sum of {0} and {1} is {2}.", firstNumber, secondNumber, firstNumber + secondNumber); Console.WriteLine("The result of subtracting {0} from {1} is {2}.", secondNumber, firstNumber, firstNumber - secondNumber); Console.WriteLine("The product of {0} and {1} is {2}.", firstNumber, secondNumber, firstNumber * secondNumber); Console.WriteLine("The result of dividing {0} by {1} is {2}.", firstNumber, secondNumber, firstNumber / secondNumber); Console.WriteLine("The remainder after dividing {0} by {1} is {2}.", firstNumber, secondNumber, firstNumber % secondNumber); Console.ReadKey(); } } } // Demonstrating Operations using System; namespace Test { class Program { static void Main(string[] args) { Console.WriteLine("Enter an integer:"); int myInt = Convert.ToInt32(Console.ReadLine()); bool isLessThan10 = myInt < 10; bool isBetween0And5 = (0 <= myInt) && (myInt <= 5); Console.WriteLine("Integer less than 10? {0}", isLessThan10); Console.WriteLine("Integer between 0 and 5? {0}", isBetween0And5); Console.WriteLine("Exactly one of the above is true? {0}", isLessThan10 ^ isBetween0And5); Console.ReadKey(); } } } // Demonstrating if else .
Nucleusoftech
5.
6.
Nucleusoftech
8.
Nucleusoftech
Nucleusoftech
11.
Nucleusoftech
14.
Nucleusoftech
16.
Nucleusoftech
17.
18.
Nucleusoftech
20.
Nucleusoftech
Nucleusoftech
Nucleusoftech
Nucleusoftech
// declare two float variables float floatValue1 = 2f; float floatValue2 = 5f; System.Console.WriteLine("initial floatValue1 = " + floatValue1 + ", floatValue2 = " + floatValue2); // swap the two float variables // (uses the Swap() method that accepts float parameters) mySwapper.Swap(ref floatValue1, ref floatValue2); // display the final values System.Console.WriteLine("final floatValue1 = " + floatValue1 + ", floatValue2 = " + floatValue2); mySwapper.Swap(ref floatValue1, ref floatValue2); } } } // More operator overloading. using System; namespace test { // A three-dimensional coordinate class. class ThreeD { int x, y, z; // 3-D coordinates public ThreeD() { x = y = z = 0; } public ThreeD(int i, int j, int k) { x = i; y = j; z = k; } // Overload binary +. public static ThreeD operator +(ThreeD op1, ThreeD op2) { ThreeD result = new ThreeD(); /* This adds the coordinates of two points and returns the result. */ result.x = op1.x + op2.x; result.y = op1.y + op2.y; result.z = op1.z + op2.z; return result; } // Overload binary -. public static ThreeD operator -(ThreeD op1, ThreeD op2) { ThreeD result = new ThreeD(); /* Notice the order of the operands. op1 is the left operand and op2 is the right. */ result.x = op1.x - op2.x; result.y = op1.y - op2.y; result.z = op1.z - op2.z; return result; } // Overload unary -. public static ThreeD operator -(ThreeD op) {
24.
Nucleusoftech
Happy C# Coding
Nucleusoftech