RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

My Simple C-Sharp Questions:

sec_goat

Squire
After trying to tackle some scripts i realise I am sorely lacking in programming knowledge so I am trying to learn C-Sharp and Instead of Spamming this forum with a new thread every time I Have a question, I will just post here and keep this thread running.

Question 1: Use of the {0} parameter

Why choose one method over the other when they both seem to give me the same results:

Code:
 x = 7;
            y = 5;

 result = x-y;
            Console.WriteLine("x-y: {0}", result);


Code:
 x = 7;
            y = 5;
result = x-y;
            Console.WriteLine("x-y: " + result);
 

Jeff

Lord
You never want to do string + string in C#. strings are Immutable.

Immutable: In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created

So, when we do string + string... in memory we have 1 strings, result gets cast to a string, and we allocate a 3rd to join them. This gets detramental when you do string+string+string+string+string+string+string.. each + allocates a new string... this is terrible.

Using the string.Format option internally uses a string builder that will allocate the entire string 1 time and insert the values of the supplied arguments.

On a side note, this is a question that has been asked in EVERY single interview I've ever gone on for C#. Not knowing that string + string is bad and why will ensure you do not get a job with the interviewing company ;)
 

sec_goat

Squire
You never want to do string + string in C#.

So using the {0}({1},{2} etc) parameter would allow me to concatenate multiple strings into whatever I am outputting without creating multiple strings. I at least understand that it is not a good practice.

Thanks Jeff!
 

Jeff

Lord
if you want to strickly concat... use string.Concat. If you need formatting... use string.format :)
 

sec_goat

Squire
Question 2: I am having trouble wrapping my head around the difference between the array types
we have
Code:
array[][] MyArray = new array[2][2]
and
Code:
array[,] MyArray = new array[2,2]

the first is an Array of arrays, and the second a 2 dimensional array.
I guess I am a bit confused on what the real difference is or why I would use one instead of another. I understand with the [][] I can specifically specify the size, and thus help to contain memory issues?

But in C++ I used the Array[][] to act as a two dimensional array, or at least that was how I understood it, perhaps I was wrong on that too?
 

Jeff

Lord
Your Array of Arrays is knows as a Jagged Array. Reason being the 2nd array doesnt have to be the same size... So, you can do

Code:
int[][] myIntArray = new int [5][];

myIntArray[0] = new int[5];
myIntArray[1] = new int[2];
//etc...
If you were to look at this in some sort of graphical format... you can think of the size of the 2nd array as a bar in a bar chart... it can look staggard...

In a multi dimensional array, the size is a constant.
 

sec_goat

Squire
If you were to look at this in some sort of graphical format... you can think of the size of the 2nd array as a bar in a bar chart... it can look staggard...

In a multi dimensional array, the size is a constant.

Very nice the visual description helps a lot for me. I can see the Jagged array as a bar graph now and the multidimensional one as a square or cube, assuming you can give it more than 2 dimensions.

Thanks again for being my teacher Jeff
 

Jeff

Lord
Yup, the real mind-bender is complete jagged arrays such as int[][][][].... which at some point makes you feel like you are doing things incorrectly... but sometimes you cannot beat the speed of an array (vs. creating complex objects to manage things easier)
 

sec_goat

Squire
So what would that be like a bar graph inside a bar graph? Man sometimes I think my poor brain isn't cut out for this stuff! I am going to take it slow and tackle this one when it comes up.
 

sec_goat

Squire
Question 3: Creating a new instance of a class

when creating a new instance of a class it seems like it is always called the same way, I am still not 100% sure why it is done this way:

Code:
MyClass myclass = new MyClass();

Why does it always start with the class name (case sensitive) followed by the class name (not case sensitive?) I understand the second is the name of the new class instance, but why is it done like that instead of just:

Code:
myclass = new MyClass();

Thanks!
 

Jeff

Lord
The first word "MyClass" is staying I'm going to make a new variable of this type. Then you have "myClass" saying you are naming said variable this name... the name doesn't have to be myClass, it could be mc, mClass, myC, doesnt matter... its your choice.. Most peoplename it the same just so they know what that class type is. Then you have "= new MyClass()" which actually creates the new instance of the object. The reason you don't want just the variable name and the instance creation "myClass = new MyClass()" is because you may want to use polymorphism... for instance, this is perfectly legitimate... "object myClass = new MyClass()". Since all types in C# implicitly inherit from object, you can do this (you probably wouldn't, but just trying to give you an idea.
 

sec_goat

Squire
That makes sense now. My problem before was that I did not realize, or remember, that you can create a new variable type based off of a class, I always remember the base variable types. I seem to always get distracted right aorund now when I try to learn programmign so the more advanced stuff like classes and pointers haven't really stuck yet.

Thanks again!
 

sec_goat

Squire
;) Anytime, glad to help, in fact, I've really enjoyed the questions you are asking.
Thanks, I really appreciate that alot! I have a kind of more general question in regards to learning this language.
I am a more hands on learner, I find I can do alright modifying scripts and the like, but I don't feel as if I am actually learning anything unless I write it myself. What did you do to learn the language, I think I recall reading that RunUO helped in that aspect? Could, or would you suggest any sort of projects or challenges that might help me to use and and advance my knowledge in C sharp?

If not I understand that as well and will continue asking questions specific to the language.

Thanks!
 

Jeff

Lord
I started really getting into programming because of 2 things, RunUO and a job I had doing Data Recovery. I've been programming since 2003 and am completely self taught... no school. The only way I learn new technology, techniques, design patterns, etc. is to apply them to a project I want to work on. First it was RunUO, learning C#, learning some design patterns. Then I moved to windows forms for work, writing utilities to help me do my job, Data Recovery helped me learn a lot of low level stuff. Then along came ConnectUO... at first I was part of a team and only did the windows forms side of things... but the team slowly dispersed and I was left to manage it all on my own which forced me to learn php and SQL.

If you want to learn or further your knowledge the only thing I can suggest is to start a project that isn't just RunUO scripting... you could even stick with the whole UO theme since I'm sure its an interest and perhaps move to 3rd party utilities. There is always the need for 3rd party stuff... The community needs some good tools because right now it's a lot of old, out-dated stuff. Just to name a few, Region Editor, Remote Administration, easier way to make maps (constantly converting bitmaps isn't ideal in my book), theres probably other things needed but I'm not really into UO anymore (other then ConnectUO) so I'm a bit out of the loop. The great thing about this community is that it isn't just limited to RunUO development, it really has moved to UO development. There are plenty of users that know more then just RunUO who can help you with questions. You could even write a utility that already exists... just to improve upon it, maybe you have a better view of how the app should work, maybe more intuitive... who knows.
 

sec_goat

Squire
I started really getting into programming because of 2 things, RunUO and a job I had doing Data Recovery. I've been programming since 2003 and am completely self taught... no school.

I wish I could say the same about my programming knowledge. I do have a pretty worthless Bachelor's degree in Software Engineering, if you can believe that by the questions I ask. Aside from that I have t say I made some good progress working with C++ and the allegro library to make some simple 2d games. I will definitely look at some projects I can come up with to start getting into the hang of things. I like the UO theme as I always had that child hood dream of being a game programmer, so we will see where it goes, and you can be sure I will be back here with many more questions.

Once again, much appreciated on all the help Jeff. I hope to someday be as much of a valuable asset to this community as you are!
 

Jeff

Lord
Software Engineering is different from Software Development though. Engineering is more design, where as Development is more implementation.
 

sec_goat

Squire
Question 4: Working with strings (Arrays and Regex?)

I am attempting to write a function that reads the lines in from a text file. Each line is stored as a separate entry in the array. The user then Inputs a word and the function will write out the line if it has a matching word. What I would like to do is take that input and only have it output the text on the line after a specific character, in my case i chose a comma, but it could be anything.

What feature, funtion, library(?) etc would I look at using to do this?

Also As a flashback to question 1, what is the difference between {0} and {0,24}, they seem to produce the same result for me.

Code:
string[] lines = System.IO.File.ReadAllLines(@"C:\Documents and Settings\My Documents\SharpDevelop Projects\Translator1.0\text.txt");

        Console.WriteLine("what word would you like to translate?");
            string word;
            word = Console.ReadLine();
            string sPattern = word;
            string[] results;
        foreach (string s in lines)
        {
 
            if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
            {
                results[] = (s);
                System.Console.Write("{0,24}", s);
                System.Console.WriteLine("  (match for '{0}' found)", sPattern);
                Console.WriteLine(results);

            }
 
Top