1 00:00:00,430 --> 00:00:01,480 ‫Welcome back. 2 00:00:01,510 --> 00:00:06,730 ‫Now that you know how to create variables, it's time to have a look at immutable values, which are 3 00:00:06,730 --> 00:00:07,780 ‫called constants. 4 00:00:07,780 --> 00:00:14,410 ‫So we are going to create variables which are not changeable after they have been created. 5 00:00:14,410 --> 00:00:21,520 ‫So the means after the compile time has started, they cannot be changed for the lifetime of the program. 6 00:00:21,520 --> 00:00:28,000 ‫And that's a very useful tool because there are some variables which you just don't want them to ever 7 00:00:28,000 --> 00:00:28,540 ‫change. 8 00:00:28,540 --> 00:00:34,030 ‫For example, pie, you don't want pie to ever change, it will always be the same or general. 9 00:00:34,030 --> 00:00:38,470 ‫There are variables which you just don't want to change and we are going to create some of those. 10 00:00:38,620 --> 00:00:43,660 ‫So let's create them and go ahead right here before our main method. 11 00:00:43,660 --> 00:00:52,240 ‫So the idea behind constants is that they usually are fields, so variables which are outside of any 12 00:00:52,240 --> 00:00:56,080 ‫method, so which are in the class, but outside of any method. 13 00:00:56,080 --> 00:01:03,010 ‫So the means we create fields now which are constants, so constants as fields. 14 00:01:03,400 --> 00:01:10,540 ‫And in order to create constants you need the const keyword and then you indicate which data type you 15 00:01:10,540 --> 00:01:10,900 ‫want. 16 00:01:10,900 --> 00:01:18,940 ‫So in my case, let's say I want to have a double which I call pie, and that value should always be 17 00:01:18,940 --> 00:01:25,360 ‫3.14159265359. 18 00:01:25,780 --> 00:01:28,420 ‫So that's a pretty precise pie. 19 00:01:28,450 --> 00:01:35,350 ‫And you can now go ahead and calculate, for example, everything that is surrounding circles with that 20 00:01:35,350 --> 00:01:36,130 ‫variable pie. 21 00:01:36,160 --> 00:01:44,470 ‫Then for example, a constant which is of type int and will be weeks because there are only a certain 22 00:01:44,470 --> 00:01:46,780 ‫amount of weeks in a year. 23 00:01:46,780 --> 00:01:53,860 ‫So let's say we want to save that as a variable and which is a field, a constant field. 24 00:01:53,860 --> 00:02:00,310 ‫And then I want to at the same time store the months as there are only 12 months in a year. 25 00:02:00,730 --> 00:02:06,220 ‫So you can simply do that within one row just like that or within one line, just like that. 26 00:02:06,760 --> 00:02:13,960 ‫And you could go ahead and say, okay, there are 365 days in a year, but then there are different 27 00:02:13,990 --> 00:02:16,900 ‫years and some years there are 366 days. 28 00:02:16,900 --> 00:02:24,070 ‫And that's why it's tricky to use days as a constant. 29 00:02:24,610 --> 00:02:31,810 ‫So let's say we set that up as 365 and now every four years we would like to change that. 30 00:02:32,020 --> 00:02:36,520 ‫We'd like to set days as being 366 days. 31 00:02:37,210 --> 00:02:43,050 ‫And now you see the left hand side of an assignment must be a variable property or indexer. 32 00:02:43,300 --> 00:02:48,940 ‫So it says we cannot change that days constant as it's constant. 33 00:02:48,940 --> 00:02:51,940 ‫So it's immutable, as I stated right here. 34 00:02:52,570 --> 00:02:52,900 ‫All right. 35 00:02:52,900 --> 00:02:53,740 ‫So don't do that. 36 00:02:53,740 --> 00:02:56,290 ‫Don't try to change constants. 37 00:02:56,590 --> 00:03:01,450 ‫And by the way, let's get rid of this days constant because it's pretty tricky, as I said. 38 00:03:01,480 --> 00:03:08,560 ‫Now for you, a little challenge create a constant as a field, which is your birthday as a string. 39 00:03:09,010 --> 00:03:13,150 ‫So go ahead and create a constant of type string with your birthday as its value. 40 00:03:15,570 --> 00:03:15,900 ‫All right. 41 00:03:15,900 --> 00:03:16,920 ‫I hope you tried it. 42 00:03:17,190 --> 00:03:25,650 ‫So, concert string and I'm going to call that birthday and it's going to be a string. 43 00:03:25,650 --> 00:03:27,240 ‫So I need to parentheses. 44 00:03:27,240 --> 00:03:33,540 ‫And here I enter 31st of May in 1988. 45 00:03:33,540 --> 00:03:36,300 ‫So now this is the structure that we have in Germany. 46 00:03:36,660 --> 00:03:39,990 ‫This is how we apply dates, all we write dates. 47 00:03:39,990 --> 00:03:43,620 ‫Of course, it could be a different one, so I'm going to copy that. 48 00:03:43,950 --> 00:03:47,430 ‫In your case, based on the country you're at, you have different standards. 49 00:03:47,430 --> 00:03:53,490 ‫So maybe you have the month first and then you have the day or even further. 50 00:03:55,540 --> 00:04:07,060 ‫Let's go ahead and use something like even the year first, then the month and then the day like that. 51 00:04:07,420 --> 00:04:11,080 ‫So there are different ways of writing a birthday. 52 00:04:11,080 --> 00:04:11,560 ‫I know that. 53 00:04:11,560 --> 00:04:13,510 ‫So either one is fine. 54 00:04:13,900 --> 00:04:21,820 ‫The important thing is that you created a constant and you created a string field, which is a birthday 55 00:04:21,820 --> 00:04:24,790 ‫field, and now you can simply print out your birthday. 56 00:04:24,820 --> 00:04:26,560 ‫So please go ahead and try that. 57 00:04:28,620 --> 00:04:34,550 ‫So see w double tab gives me the console right line and I'm here, I'm going to enter. 58 00:04:34,560 --> 00:04:48,570 ‫My birthday is always going to be and then here I use the curly brackets and the zero in order to add 59 00:04:48,570 --> 00:04:49,860 ‫a variable afterwards. 60 00:04:49,860 --> 00:04:55,860 ‫So I'm going to add a variable at that position here at the zero position, which is going to be my 61 00:04:55,860 --> 00:04:57,630 ‫birthday constant. 62 00:04:57,960 --> 00:04:58,560 ‫All right. 63 00:04:58,710 --> 00:05:01,320 ‫So now let's run that for that. 64 00:05:01,320 --> 00:05:08,670 ‫We need to console the read key, for example, so that our application doesn't stop. 65 00:05:09,390 --> 00:05:10,560 ‫And we are. 66 00:05:10,680 --> 00:05:15,330 ‫My birthday is always going to be the 31st of May 1988. 67 00:05:15,750 --> 00:05:16,680 ‫Okey dokey. 68 00:05:16,680 --> 00:05:22,680 ‫So now that you know how to create constants and how to use them, please go ahead with the next video. 69 00:05:22,680 --> 00:05:31,020 ‫And keep in mind, simply use the const keyword before the data type whenever you want to use constants.