Online Workshop: Vibe Coding for Production: https://dometrain.com/workshop/vibe-coding-for-production/?ref=nick-chapsas&promo=mail-list&coupon_code=3YEARS
Get every Dometrain 40% off with code 3YEARS: https://dometrain.com/courses/?ref=nick-chapsas&promo=youtube&coupon_code=3YEARS
Hello, everybody. I'm Nick, and in this video, I want to show you how you can use the brand new .NET scripting capabilities with file references, which was just added in .NET 11 preview 3.
Don't forget to comment, like and subscribe :)
Social Media:
Follow me on GitHub: https://github.com/Elfocrash
Follow me on Twitter: https://twitter.com/nickchapsas
Connect on LinkedIn: https://www.linkedin.com/in/nick-chapsas
#csharp #dotnet
Оглавление (2 сегментов)
Segment 1 (00:00 - 05:00)
Everybody, I'm making finally the most innovative feature of. NET, which in my opinion should have been there from the very beginning, but we got it in. NET 10. File-based apps is actually finally useful now. And this feature I'm going to show you today doesn't just apply in the new. NET 11 release because yes, it is coming in the. NET 11 preview 3 that just came out, but will actually be back ported into the LTS version of. NET 10. So, you can use exactly what you're going to see here when that version is out too. Now, it is still in preview technically, but it truly makes file-based apps useful. So, what are file-based apps really quickly? All I have here is just a single file, app. cs, nothing else. It's just a C# class. And it's an empty file. Now, ignore this NuGet. config. All it really has is configuring. NET 11's NuGet feed so I can actually pull the NuGet packages. Normally, you wouldn't need that. Uh and I'm going to go into this app. cs and let's say I want to start scripting C# just very quickly prototyping, writing something. I can say console. writeline, the simplest example in the world, and then hello world. If I could probably spell it, I cannot. So, if I go ahead and I just save this and I say dotnet run, nothing will happen because this is not a CS proj. This doesn't have a solution, a project, or anything. Just a single file. But if I say app. cs, then that file will run. You're going to get hello world. And the first time it's going to restore and make sort of a virtual uh project to get this to work. And then you just going to get the response back. And there's some really cool things you can do with this. For example, do you want to bring a NuGet package? You can use the package directive, and then you can say give me whatever version of Spectre. Console, the latest in this case. Do I want to change a project property and say that this should be targeting. NET 10 explicitly? I can do it like this. And that's on the entry point file. I can even say, "Hey, this project should be referring to another project that's located in this location. " So, you had quite a lot of flexibility with what you could do. However, what you couldn't do is say that, "Oh, I have another file called shared constants. cs over here that I want to share between my stuff and I'm going to have like a public class. We're going to call it shared constants and then I'm going to just have a bunch of things here. Let's say public static or public const string greeting. " And if I wanted my Hello World, and this of course will work with method properties, whatever, right? And I wanted that to be usable in here, well, I couldn't. This can't really say, "Oh, I'm going to use this because it just happens to be in the same location. " It doesn't really know where it is. It doesn't exist. And I can't really say using whatever namespace. However, now in this. NET 11 preview 3 version, we do get a way to do this. And we actually it even deeper. Let me explain. So, first and foremost, if I want to use that file, all I have to say now is say hashtag include and then I'm going to point to the file. In this case, because we're on the same location, I'm going to say shared constants. cs and that is technically it. As you can see, this now figures it out. I don't think this version of the compiler knows what include means yet, but if I go and I run it, it will actually fail because I have to explicitly enable this experimental feature. The way to do this is the way we would do a property as well before. So, we're going to say property experimental file based program and enable include directive. Jesus Christ, what a name is that? Uh and if I do that, then this is going to run and work and say, as you can see again, Hello World. So, it will go and include that file. And for rapid prototyping, this is awesome. Now, finally, you can start splitting things as you go. And the idea is that maybe yeah, you start with a long app. cs file, but then as things grow and as you have sort of nested classes and stuff, then you start splitting things into separate files. And eventually the idea is that you're going to take that project and it will be converted into a proper project where you can detect all the properties, all the file relationships, you can include anything else like namespaces and all that and graduate into a proper project. But what happens if you want to have a nested thing in a nested thing? So, a file in a file and you refer to that. Well, you'd think it would just work by default. So, if I said public string maybe public static string get greeting or like generate the greeting and I said return hello world. And then I want to have something else in here that maybe is coming from the number keep. I don't know. Don't ask me. Then I can say public class keep and then public const int number. So, what this number is going to be I
Segment 2 (05:00 - 07:00)
don't know. Let's say this one. Here we go. And then in here I'm going to say number. However, as you can see this is not really recognized. If I go and I run it, I'm going to get another warning that hey, this does not exist in this context. Yeah, that doesn't exist because I haven't said include that file. So, I can say that go ahead and include the keep or the number keep. cs. That's cool. But then I get another warning that says oh, experimental file-based program ah it's so it's again the same thing as I have here. Well, actually it is not because if I added here and I run it again, it's not going to work. Why? Because this is another experimental feature. So, it's not just the entry point experimental feature, you also have the nested transitive directives that you need to enable. So, we're going back over here to the app. cs and I'm going to paste that here slightly different. This is include, this is transitive. That's fine. Once you have that, then this will go ahead and it will run and you can go and say generate and call it here. Save, run and get the hello world with the number. Pretty cool as we finally have this. Again, one step to bridge the gap between, "Hey, I can do some very prototype-y style of scripting and graduating into a full project. " I don't know what is next to come. Maybe a way to sort of convert an app like this to a CS prod proper app. We'll see, but for now, that's where we are. Now, if you made this far into the video, I'd like to let you know that Don't Train celebrate our third anniversary, which is an insane milestone to hit. And for that, we're giving 4% off everything, any course, any Don't Train Pro. We just launched a few massive, massive courses, like 10 courses or 12 courses in the last 2 months. And we finally launched our authentication course. That's a beast of a 12-hour course. If you want to check it out, I'm going to put some links in the description. And that is it. But, I really want to know from you, what do you think about this feature and this feature in general? And should Microsoft be focusing on something like this? Leave a comment down below, let me know. Well, that's all I have for you for this video. Thank you very much for watching. As always, keep coding.