Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
4.3 Static typing: benefits, drawbacks and ways to minimise them
//4.3 Static typing: benefits, drawbacks and ways to minimise them using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //C# is a language with static typing. //This means that the type of every variable or expression is known at compile time. //This is what makes tools such as intellisense (code completion), navigation to type definition, //automatic namespace resolution and many others possible. //To me this is something that makes code more readable too. //Because of this there is no language built-in way to define types dynamically (i.e. during execution) //For example in javascript you could do something like that (http://rextester.com/BBDYW2959) //var b=user_inputed_value, c=user_inputed_value; //var a = {}; //if(b < c) //{ // a.Thing = {Some: "test", Property: 25}; //} //else //{ // a.Thing = {SomeThing: "enirely", Else: "."}; //} //print(a.Thing.Some) // //So what is a.Thing is not known at compile time and is totally dependent on a runtime value of b < c //This is probably a bad thing to do anyway, so C# won't let you do that (easily at least). //However sometimes you would need a class quickly just for something local. For that there are nested classes, i.e. classes defined in another class. var fruit = new Fruit(); fruit.UsingNestedClass(); //Another times you don't even want to define a class for something temporary. You could use anonymous types. //These are special classes that compiler will define for you, and will give some name to them. Everything is still known at compile time. var anonym = new { SomeString = "string", Number = 5 }; Console.WriteLine(anonym.SomeString); Console.WriteLine(anonym.GetType()); //Finally you could extend types using partial classes. So if you have a class with partial modifier (as is ExtendMe class below) //you could define another class with this name somewhere else in the same namespace and add more functionality to it (as does the second declaration of ExtendMe class) var ex = new ExtendMe(); Console.WriteLine(ex.Property); Console.WriteLine(ex.Hm()); //These features and some others (like var keyword) makes C# feel like dynamically typed language (in a good sense), while still keeping it statically typed. } } public class Fruit { public void UsingNestedClass() { var nested = new SomeNestedClass(); Console.WriteLine(nested.Whatever()); } class SomeNestedClass { public string Whatever() { return "Whatever"; } } } public partial class ExtendMe { public string Property = "Property"; } public partial class ExtendMe { public string Hm() { return "Hm"; } } }
run
|
edit
|
history
|
help
0
Ki
asxsv sdsd
dynamic object
Events
Hehd
utf-8 string to bytes and back
30272 Program Ex6_1 else_if
jjj
перегружаемый оператор
Linq join to tables