-
Jul 24th, 2022, 08:06 AM
#1
Thread Starter
Frenzied Member
Some verbose C# code
Some people say that C# code is very concise and elegant. But the ubiquitous "casting" in C# code makes C# code seem verbose and bloated.
C#
Code:
private static RuntimeType Type_RuntimeType = (RuntimeType)typeof(RuntimeType);
private static RuntimeType Type_Type = (RuntimeType)typeof(Type);
VB.NET
Code:
Private Shared Type_RuntimeType As RuntimeType = CType(GetType(RuntimeType), RuntimeType)
Private Shared Type_Type As RuntimeType = CType(GetType(Type), RuntimeType)
-
Jul 24th, 2022, 08:44 AM
#2
Re: Some verbose C# code
Any strongly-typed language will require some casting but well-written code requires very little. Casting in C# is more concise than in VB and as concise as in any other C-based language. I'm not really sure what point you're trying to make. Is your issue with casting in general or with how casting is done in C#?
-
Jul 24th, 2022, 11:25 AM
#3
Re: Some verbose C# code
Originally Posted by SearchingDataOnly
Some people say that C# code is very concise and elegant. But the ubiquitous " casting" in C# code makes C# code seem verbose and bloated.
C#
Code:
private static RuntimeType Type_RuntimeType = (RuntimeType)typeof(RuntimeType);
private static RuntimeType Type_Type = (RuntimeType)typeof(Type);
VB.NET
Code:
Private Shared Type_RuntimeType As RuntimeType = CType(GetType(RuntimeType), RuntimeType)
Private Shared Type_Type As RuntimeType = CType(GetType(Type), RuntimeType)
Not really sure what you are trying to prove there but I don't think I have ever seen such a mix of casting and typeof operators when using C#, although admittedly some WPF code involving DependencyProperties can be really ugly.
-
Jul 25th, 2022, 12:17 AM
#4
Thread Starter
Frenzied Member
Re: Some verbose C# code
Originally Posted by jmcilhinney
Any strongly-typed language will require some casting but well-written code requires very little. Casting in C# is more concise than in VB and as concise as in any other C-based language. I'm not really sure what point you're trying to make. Is your issue with casting in general or with how casting is done in C#?
I think a really concise and elegant language should look like this:
C#
Code:
private static RuntimeType Type_RuntimeType = typeof(RuntimeType);
private static RuntimeType Type_Type = typeof(Type);
VB.NET
Code:
Private Shared Type_RuntimeType As RuntimeType = GetType(RuntimeType)
Private Shared Type_Type As RuntimeType = GetType(Type)
I don't know if the above code will throw an error in VS-IDE.
-
Jul 25th, 2022, 12:21 AM
#5
Thread Starter
Frenzied Member
Re: Some verbose C# code
Originally Posted by PlausiblyDamp
Not really sure what you are trying to prove there but I don't think I have ever seen such a mix of casting and typeof operators when using C#, although admittedly some WPF code involving DependencyProperties can be really ugly.
My sample code is from Reflection\CustomAttribute.cs in Microsoft .NET4.8
-
Jul 25th, 2022, 12:57 AM
#6
Re: Some verbose C# code
Originally Posted by SearchingDataOnly
I think a really concise and elegant language should look like this:
C#
Code:
private static RuntimeType Type_RuntimeType = typeof(RuntimeType);
private static RuntimeType Type_Type = typeof(Type);
VB.NET
Code:
Private Shared Type_RuntimeType As RuntimeType = GetType(RuntimeType)
Private Shared Type_Type As RuntimeType = GetType(Type)
I don't know if the above code will throw an error in VS-IDE.
On what basis do you justify that? They would have to code a special case into the compiler just for that. Special cases are considered bad unless they provide significant value, which this would not. As far as I can tell, RuntimeType is not even something that the rest of us can access, so it would be of even less value.
-
Jul 25th, 2022, 12:58 AM
#7
Re: Some verbose C# code
How about you show us a code sample that is representative of what the average .NET developer would write that exemplifies what you consider to be unnecessary casts and justify your position with something more than "I don't like it".
-
Jul 25th, 2022, 07:34 AM
#8
Thread Starter
Frenzied Member
Re: Some verbose C# code
I'm not trying to prove anything, just having some thoughts while reading the C# code, just wondering why it does it this way, or why it doesn't do it better?
Whether I like C# or not, it's just a reference for me, one of dozens.
-
Jul 25th, 2022, 07:52 AM
#9
Re: Some verbose C# code
If you have a question to ask, why isn't it asked in post #1? If you're just musing, why do we need to know about it? If you want to know how and why casti8ng works, there's plenty of places you could read about it. If you already understand casting then you should understand how and why this works as it does. As I have already said, making it work the way you suggest would require coding a special case and Microsoft justifiably prefer to avoid doing that unless it provides significant value. This would not. Which part is confusing you?
-
Jul 25th, 2022, 08:45 AM
#10
Thread Starter
Frenzied Member
Re: Some verbose C# code
I'm thinking about the cost of explicit type and if there is a better solution than C# when it comes to type casting.
Some people say that C# is the ceiling of object-oriented programming, but others say that object-oriented programming is the ceiling of C#.
Last edited by SearchingDataOnly; Jul 25th, 2022 at 08:53 AM.
-
Jul 25th, 2022, 10:40 AM
#11
Re: Some verbose C# code
Some say trying to sound deep makes you sound shallow. There are many different languages that work many different ways. C# works the way it works. Maybe you should try some other language(s) if you think something else would be better.
-
Jul 25th, 2022, 10:46 AM
#12
Re: Some verbose C# code
Originally Posted by SearchingDataOnly
I'm thinking about the cost of explicit type and if there is a better solution than C# when it comes to type casting.
Some people say that C# is the ceiling of object-oriented programming, but others say that object-oriented programming is the ceiling of C#.
Not sure what you mean by "the cost of explicit type". Being more explicit about data types and having to tell the compiler that a risky assignment is okay might be a cost to the programmer as it is slightly more typing and a bit more thinking; however it will reduce the amount of runtime errors caused by assigning incompatible types (or at least types with a risk of data loss).
The compiler should be able to strip the casting code out anyway, it is more of an instruction to the compiler to tell it that you meant what you typed.
For example if I did the following C#
Code:
int x = int.MaxValue;
long y = long.MaxValue;
y = x;
x =(int) y;
The resultant IL (with my added comments) is
Code:
.method private hidebysig static void '<Main>$' (string[] args) cil managed
{
.maxstack 1
.entrypoint
.locals init (
[0] int32 x, //allocates an int32 for var x
[1] int64 y //allocates an int64 for var y
)
//following two lines initialise var x
ldc.i4 2147483647
stloc.0
//following two lines initialise var y
ldc.i8 9223372036854775807
stloc.1
//following 3 lines assign the variable x (an int) to variable y (a long)
ldloc.0
conv.i8
stloc.1
//following 3 lines assign the variable y (a long) to variable x (an int)
ldloc.1
conv.i4 //note no cast needed at the IL level, it is purely a compiler thing
stloc.0
//exit
ret
Last edited by PlausiblyDamp; Jul 25th, 2022 at 06:52 PM.
-
Jul 27th, 2022, 08:12 AM
#13
Thread Starter
Frenzied Member
Re: Some verbose C# code
When I say cost, I mean: because c# requires explicit types, the code is full of a large number of type conversion functions, which makes the code bloated and verbose. This makes C# unable to be called a concise and elegant language.
Corresponding to C#, JavaScript and VBScript are at the other extreme in type handling, maybe we should find a balanced solution.
Last edited by SearchingDataOnly; Jul 27th, 2022 at 08:17 AM.
-
Jul 27th, 2022, 09:28 AM
#14
Re: Some verbose C# code
Gee, if only there were loads of different programming languages with loads of different characteristics and you could pick whichever one you want to use.
-
Jul 27th, 2022, 12:09 PM
#15
Re: Some verbose C# code
Originally Posted by SearchingDataOnly
When I say cost, I mean: because c# requires explicit types, the code is full of a large number of type conversion functions, which makes the code bloated and verbose. This makes C# unable to be called a concise and elegant language.
Corresponding to C#, JavaScript and VBScript are at the other extreme in type handling, maybe we should find a balanced solution.
Explicit types are a deliberate choice, they prevent a lot of errors caused by a lack of strong typing or automatic conversions when assignments could cause data loss.
Typically though you don't see a lot of casting in well written C# code, there are always times when it is needed but I don't find it is something I encounter a lot (often it is a sign of a poor design choice elsewhere). Type conversion functions are another matter - although things like int.TryParse and similar aren't what I would call overly verbose or bloated code though - especially as they help reduce errors.
-
Jul 27th, 2022, 12:39 PM
#16
Re: Some verbose C# code
Code:
class MyBaseType
{
}
class SomeType : MyBaseType
{
}
...
var a = new SomeType();
var b = a as MyBaseType;
if (b is not null) {
// If b is not null then it is declared as MyBaseType
}
if (a is MyBaseType c) {
// if a is MyBaseType or derived from MyBaseType - use c as MyBaseType
}
No need of typeof() checks and oldschool C-like casting. C# is modern language in version 10 (and 11 is on the way to be released this year).
-
Jul 27th, 2022, 12:45 PM
#17
Re: Some verbose C# code
And some real-world example:
Code:
List<string> a = new();
var b = a as IEnumerable<string>;
if (b is not null) {
// executed as check is true
Console.WriteLine("b is not null");
}
if (a is IEnumerable<string> c) {
// executed as above check is true (a is of type IEnumerable of string)
Console.WriteLine("a is IEnumerable<string> c");
}
if (a is IEnumerable<int> d) {
// not executed
Console.WriteLine("a is IEnumerable<int> d");
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|