0% found this document useful (0 votes)
3 views

Program_L

The document is a C# program that demonstrates various data types, variable declarations, and operations. It covers integers, floating-point numbers, arrays, strings, and methods for manipulating these data types. Additionally, it includes examples of comments, variable initialization, and basic arithmetic operations.

Uploaded by

Mehmet Yılmaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Program_L

The document is a C# program that demonstrates various data types, variable declarations, and operations. It covers integers, floating-point numbers, arrays, strings, and methods for manipulating these data types. Additionally, it includes examples of comments, variable initialization, and basic arithmetic operations.

Uploaded by

Mehmet Yılmaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1 using System;

2 using System.Linq;
3 using System.Collections.Generic;
4
5 namespace HelloWorld
6 {
7 public static class Program
8 {
9 public static void Main()//Comments can be put after a statement too
10 {
11 //Use two forward slashes for comments
12 /*You can use this one for
13 for multiline comments
14 */
15
16 //To declare a variable first write data type for the variable then the name
17 int UsrAge = 478; //Integer
18
19
20 //You can't give a floating point number to "integer" but can give an integer to
a floating point data type variable
21
22
23 //"byte" is almost same as int but has a more narrow range(0-255)
24 byte ByteVar; //Byte
25 ByteVar = 232;
26
27 //"float" refers to floating point numbers, it's range is -3.4×10^38-3.4×10^38
28 float FloatVar = 899112.67288f; //"float" rounds off numbers more than 7
digits
29
30 /*"double" is the default floating point data type in C#
31 "double" has a more wide range than "float" ([(+/-)5.0×10^-324]-
[(+/-)1,.7×10^308])
32 It's precision is about 15 to 16 digits
33 */
34 double Floatlike;
35 Floatlike = 87738.8387;
36
37 //"decimal" is a floating point data type too but has a more narrow range and
greater precision (28-29 digits)
38 decimal MorePrecise;
39 MorePrecise = 736.879283737377288m;
40 //"char" stands for character and stores one Unicide character
41 char Lttr;
42 Lttr = 'A';
43
44 //"bool" stands for boolean and can only hold two values: "true" and "false"
45 bool AnDu;
46 AnDu = true;
47
48 //Initializing a variable: Giving a variable an initial value
49 //This can be done in two ways:
50 //At the point of declaration
51 //Or in a seperate statement
52
53 //You can declare more than one variable in just one statement
54 int MyAge = 18, MyHeight = 173;
55 Console.WriteLine(MyAge);
56 Console.WriteLine(MyHeight);
57 MyAge = MyHeight;
58 Console.WriteLine(MyAge);
59 Console.WriteLine(MyHeight);
60 MyAge = 18;
61
62 //Add = +, Substract = -, Multiply = *, Divide = /, Modulus = %
63 // += -= *= /= %=
64 // x++ or ++x equals x = x+1 or x += 1
65 // x-- or --x equals x = x-1 or x -= 1
66 MyAge++;
67 Console.WriteLine(MyAge);
68 ++MyAge;
69 Console.WriteLine(MyAge);
70
71 //Sometimes it is important where you put "++"
72 MyAge = 18;
73 Console.WriteLine(MyAge++);
74 MyAge = 18;
75 Console.WriteLine(++MyAge);
76
77
78 //To convert a data type to another:
79 int x = (int)63.83663;
80 Console.WriteLine(x);
81 float Num1 = (float)76.773673; //Default floating data type in C# is "double"
82 decimal Num2 = (decimal)82.5619082;
83 Console.WriteLine(Num1);
84 Console.WriteLine(Num2);
85
86 //There are more data types: Arrays, Strings and Lists
87 //Arrays are collections of data
88 int[] UserAge = { 76, 52, 89, 16, 21, 36, 27 };
89 UserAge = new[] { 12, 32, 25, 32 };
90 //We can initialize an array with default values
91 int[] Array1 = new int[5];
92 //Default value of this array is {0, 0, 0, 0, 0}
93 //If we want to update an individual value we can access it
94 Array1[0] = 23; //We have changed the first value
95 //If we write:
96 Array1[2] = Array1[2] + 35;
97 //We add 35 to the third value
98
99 //"Length" is a property
100 Console.WriteLine(UserAge.Length);
101
102 //"Copy()" is a method, it has paranthesis. It can have three arguments
103 //You can use it as "Array.Copy(source, dest, count)"
104 Array.Copy(Array1, UserAge, 3); //We have changed first three elements of
"UserAge"
105
106 //We can sort our array with "Sort()", the array will be sorted in ascending
order
107 Array.Sort(UserAge);
108
109 //With "IndexOc()" we can determine if a certain value exists in an array
110 //If array has that value the method will return its first occurens location
111 //If it doesn't have that value it will return "-1"
112 Console.WriteLine(Array.IndexOf(Array1, 0));
113
114 //We can assign this number to a variable
115 int ans = Array.IndexOf(Array1, 0);
116 Console.WriteLine(ans);
117
118 //Strings has to be enclosed in double quotes
119 string aMessage = "Hello there";
120 //And we can concatenate strings with "+" sign
121 string newMessage = "General " + "Kenobi";
122 //We can use "Length" property with strings
123 Console.WriteLine(newMessage.Length);
124
125 /*"Substring()" method requires two arguments. First one is
126 the starting point, second one is for length
127 */
128 string rank = newMessage.Substring(0, 7);
129 Console.WriteLine(rank);
130
131 //Difference between "Write()" and "WriteLine()"
132 //"WriteLine()" provides a new line when "Write()" doesn't
133 Console.Write(2);
134 Console.Write(9);
135
136 Console.WriteLine();
137 Console.WriteLine(8328);
138 Console.WriteLine(8273668);
139
140 string str1 = "Español";
141 string str2 = "English";
142 //"Equals()" is a method that returns a boolean
qua s() s a et od t at etu s a boo ea
143 bool state1 = str1.Equals("Español");
144 bool state2 = str1.Equals(str2);
145 Console.WriteLine(state1);
146 Console.WriteLine(state2);
147
148
149
150
151 Console.WriteLine("Hello World!");
152 //Console.ReadKey();
153 }
154 }
155 }

You might also like