|
As a former C++ guy myself who still dabbles, I heartily disagree. While it would be nice to have certain C++ features in C#, some aren't there for a good reason. Sometimes you have to step back and look at those reasons, instead of ignoring them and cursing the compiler or language.
By the statements and questions you posed, I also suspect you haven't explored all C# has to offer. Learn the ropes before cursing it, you'll be surprised to find C# to be a beautiful, elegant language.
Now if you're really interested in answers to your questions, I'd be glad to answer them. But I suspect this was more a rant than a Q & A, yes?
Skippums wrote: Oh well, I guess that I will have to start implementing my own compiler so I don't have to put up with the lack of thought and foresight by the "greatest minds" in our field.
Blasphemy! Just kidding. Seriously though, the folks that designed the C# language are smart, smart folks. Anders Hejlsberg, the lead architect, also designed Turbo Pascal way back in the day, which became a smash success. The man is a seriously smart guy. No offense, but I honestly think everyone in this forum doesn't approach his wits when it comes to language design.
|
|
|
|
|
If you know why they didn't include multiple inheritence and typedef'ing, I would be very interested in hearing (or reading) it.
Jeff
|
|
|
|
|
Maybe they where just copying off java on that one. C# is more of a mix between java and c++ than a direct upgrade from c++.
My current favourite word is: PIE!
I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for.
-The Undefeated
|
|
|
|
|
SK Genius wrote: C# is more of a mix between java and c++
I agree. After being a C++ programmer for 15+ years and using java in college, I find C# to be a happy median between the two.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
typedefs are plain ugly. They are a holdover from C, I thought.
multiple inheritance was not included probably because the design focus of C# is to make it hard to hurt yourself using it. I disagree with them, but that is their focus.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
I agree that they CAN be ugly, but all I want is to do something like, "typedef UniqueId as int;", so anywhere I put the word UniqueId, the compiler will know I want it to be an int. I don't understand how strongly coupling my UniqueId property and the integer type within the class is at all helpful. Code maintanance on this is going to be atrocious.
Jeff
|
|
|
|
|
You could in fact create a generic class and define it that way. IMO maintenance on a project where the nature of your unique Id could change in mid stream is no less problematic.
Hell, you can store an object if you want, and then handle it as whatever type you like from there. But if you use generics, then your type won't be boxed if it's a value type.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi Jeff,
if you want to be able to write
for(uniqueID id=0; id<10; id++) Console.WriteLine(id.ToString());
then just do it. All it takes is a simple:
using uniqueID=System.Int32;
AFAIK this has been available since day one.
The one thing now really missing from C/C++ is you can't collect statements like the latter
in a single header file and get them included everywhere.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Yeah, it's just that I will still have to have that statement in EVERY file where I reference the type. For maintanence purposes, I will still have to change the using statement in every file. I will probably just end up making my own value type, storing the "real" member as private, then implement the IEquatable and IComparable interfaces, so I can still sort and test for equivalence. Also, this will allow for strong type checking, as apposed to typedef (and I think, the using statement as outlined above) where the types are actually equivalent. Anyway, thanks for the input!
Jeff
|
|
|
|
|
Christian Graus wrote: typedefs are plain ugly. They are a holdover from C, I thought.
Yes, they are ugly. I have always thought that they were carried over from C to maintain portability between older legacy C code and newer C++ code.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Well typedef s are prettier than having to say struct X all over the place.
|
|
|
|
|
bingo....your particular desired usage of typedefs typically follows a procedural mentality. ...C++ allows them because C++ is the disgusting bastard child caught in between the (now very)distant past of procedural design and what has been come to be accepted(OOP) as the way things are done from here(actually...probably closer to 10 years ago) until something better can be thought of ...C# is not the problem for placing said restrictions on you...C++ was the problem for allowing you to do them in the first place
"I need build Skynet. Plz send code"
|
|
|
|
|
Why didn't they include multiple inheritence: Big question. Best I can do is point you to an in-depth post by one of the CLR guys who explains in detail why they didn't: http://blogs.msdn.com/csharpfaq/archive/2004/03/07/85562.aspx[^]
Why didn't they include typedef's: well, technically they did, they just limited the scope of typedefs to the file level, which is a good thing IMO.
For example, you can write:
using IntList = System.Collections.Generic.List<int>;
...
public void Foo()
{
IntList list = new IntList();
list.Add(5);
...
}
If you don't like the fact that it's limited to the file level, then I'd just write a value type (e.g. a struct) that holds whatever data you need.
|
|
|
|
|
Skippums wrote: the removal of the ever useful typedef
"When Anders closes a door he usually opens a window someplace."
No one showed you the C# way? Won't this work for you?
namespace Test
{
class Test
{
private static MyType
F
(
MyType x
)
{
return ( x + x ) ;
}
[System.STAThreadAttribute]
static void
Main
(
string[] args
)
{
try
{
System.Console.WriteLine ( F ( 2 ) ) ;
}
catch ( System.Exception err )
{
System.Console.WriteLine ( err.Message ) ;
}
return ;
}
}
}
Of course C/C++ header files make using typedefs easier, whereas each C# file would need to have the using directive. On the other hand, you can pass C# code through the C-preprocessor if you want.
|
|
|
|
|
"you can pass C# code through the C-preprocessor if you want"
That is an interesting idea... I will have to try that one at home sometime. As far as the using statement, that works alright, but I will still have to modify the value in a number of places after the fact. What I really wanted was either namespace-scoped typedefs or project-scoped typedefs (solution-scoped would probably be nice in certain situations, but I could get by with just the other two mentioned above). Anyway, thanks for your input!
Jeff
|
|
|
|
|
Skippums wrote: I will still have to modify the value in a number of places after the fact
Wouldn't that be the case anyway?
Skippums wrote: thanks for your input!
Glad to be of service.
|
|
|
|
|
Ok, you got me. I should have said, "I will still have to modify the TYPE in a number of places after the fact".
Jeff
|
|
|
|
|
Never hate any languages,pls.
Cheers
Bino
www.codepal.co.in
|
|
|
|
|
Thanks to everyone who responded to my post! I got a lot of good opinions and dialogs going, and even some reasons why particular language design choices were made (although I still disagree with them). Perhaps the title of the post should have been, "Some of the features I miss from C++", as I still plan on using C# for my projects that I do in my free time, even when someone doesn't force me to. There are many great features of C# to take advantage of, and despite the few drawbacks (in my opinion, pretty significant drawbacks), it really is a great language. Thanks again everyone!
Jeff
|
|
|
|
|
hi,
How can I get all names of files which have extension of mp3 from filesystem (c:\ part of computer for example). I can get only the names of files with extension of mp3 in only one folder by using beloved code. By this code,I can't get, if there are mp3 files in subfolders, but I want to get all mp3 files' names under c:\ part of computer.
private void button1_Click(object sender, EventArgs e)
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\");
System.IO.FileSystemInfo[] fi = di.GetFileSystemInfos();
foreach (System.IO.FileSystemInfo f in fi)
{
if (f.Extension==".mp3")
{
richTextBox1.Text += f.Name;
richTextBox1.Text += Environment.NewLine;
}
}
richTextBox1.SaveFile(@"C:\mp3ler.xls",RichTextBoxStreamType.PlainText);
}
|
|
|
|
|
Recursion:
void GetMP3(string Path)
{
...
foreach(FileSystemInfo f in fi) {
if(f.Extension == ".mp3") {
richTextBox1.Text += f.Name;
richTextBox1.Text += Environment.NewLine;
}
}
foreach(DirectoryInfo d in di) {
GetMP3(d.FullName);
}
...
}
The function will keep calling itself for all subdirectories within a folder, quite clever when you think about it.
That code might not work, but you get the idea.
You may also need to call Application.DoEvents() somewhere too, so that your window doesn't lock up while you trawl through the compy looking for mp3's
My current favourite word is: PIE!
I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for.
-The Undefeated
|
|
|
|
|
Actually, Application.DoEvents isn't really the best way to handle that. You're absolutely right that the application should yield to the UI thread so the application remains responsive, but Application.DoEvents forces the application message pump to run and isn't recommended.
Ideally the code above should be performed on a separate thread from the main UI thread. There are many ways to do that including creating a new thread, using the ThreadPool or asynchronous execution of a delegate (delegate.BeginInvoke / EndInvoke). But one of the coolest ways to execute code off the UI thread is the Dispatcher class. You can use the Dispatcher to execute code with various priority levels and even postpone execution until the application is idle. Note that Dispatcher is new to .Net 3.0, so if you're tied to 1.x or 2.0 then you won't be able to use it. You can read about the Dispatcher here[^].
Unfortunately, once this code is running on a separate thread you still have to get back to the UI thread to update controls. If you have a listbox, for example, and you want to add a ListboxItem for each file you find you won't be able to do it from the worker thread. You can get around that using Control.Invoke, but there's an even easier way if you're in .Net 3.0 or later. That's the BackgroundWorker[^] component, which will help you in easily setting up the background thread to execute your code and even gives you a very easy way to communicate back to the UI thread. BackgroundWorker is certainly the easiest, but if it's too restrictive for what you're trying to do make sure you check out some of those other options.
Regards,
Jared Bienz
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement. Sites of Interest: MSDN Events | US ISV Team Blog
|
|
|
|
|
Be sure to run the code from the prior post in a separate thread, or Windows will assume your program is "hung" for long directory searches (such as the entire C drive), and when you do a ctl-alt-del your program will appear in the list as not responding (if not in a separate thread).
Jeff
|
|
|
|
|
Thats why i added in to use Application.DoEvents() . It gives control to the main window to do what it needs to, process messages, redraw, etc.
Though i agree that you should probably put it in a separate thread, although that will make it a little more difficult to update your textbox, though it shouldn't be too much trouble.
My current favourite word is: PIE!
I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for.
-The Undefeated
|
|
|
|
|
hi,
Directory.GetFiles(string path, string searchPattern, SearchOption searchOption)
searches recursively if you ask for it.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|