TIGSource Forums

Developer => Technical => Topic started by: rivon on September 28, 2012, 10:27:59 AM



Title: Quick and easy C# tutorial for a C++ guy?
Post by: rivon on September 28, 2012, 10:27:59 AM
Hey guys, I need to learn C# in a week. I know C++ quite good and I know Java a bit. Do you know any good and quick tutorials for a guy like me?


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: kamac on September 28, 2012, 10:45:54 AM
This will probably do.

http://msdn.microsoft.com/en-us/library/aa288436(v=vs.71).aspx


You just have to go through each one of them.
You're lucky, as C# is a combination of C++ and Java  ???


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: ChevyRay on September 28, 2012, 11:10:48 AM
Variables

Code:
int myInt = 0;
float myFloat = 0.5f;
double myDouble = 0.5;

const int foreverFour = 4;

float a = 0; //no problem
double b = 0.5f; // no problem
int c = 0.5f; //error time!

Arrays

Code:
int[] myInts = new int[10];
myInts[0] = 10;

float[] myFloats = { 1.0f, 2.2f, 0.9f, -4.3f };

Functions

Code:
void MyFunction()
{

}

public float MyPublicFunction(float x, float y)
{
return x * y;
}

Classes & Read-only properties

Code:
public class MyClass
{
//private member
private float health = 15;

public MyClass()
{

}

//public accessor, can only set internally
public float Health
{
get { return health; }
private set { health = value; }
}
}

Reference types vs. Value types

Code:
//Value types
int a = 5;
int b = 5;
a = 10;
//a != b, a is now 10 and b is 5

//Reference types (instead of pointers)
MyClass myObj = new MyClass();
myObj.DoSomeFunction();

//No need to delete/destroy/etc., just null stuff out and garbage collector cleans up
myObj = null;

Structs

Code:
public class Test
{
public Test()
{
Vec2 a = new Vec2(10, 10);
Vec2 b = a;
a.X = 5;

//a != b
//a is now (5,10)
//b is now (10,10)
//structs are *value* types, so they are copied like int, string, etc.
}
}

public struct Vec2
{
float X;
float Y;

public Vec2(float x, float y)
{
X = x;
Y = y;
}
}

Loops

Code:
for (int i = 0; i < 10; i++)
{
//the usual, "i" is scoped *to* the loop alone and can't be used afterwards
}

//can also use foreach to loop through arrays/lists
int[] list = {0, 10, -25, 14, 22};

foreach (int value in list)
{
//a == 0, 10, -25, 14, 22
//will loop in the array order
}

Surf the msdn docs like crazy, it's extremely well-documented and has tons of easy examples. Also use System.Collections.Generic classes (List, Dictionary, HashTables, etc.) and all that good stuff, it's got built-in sorting.

You can overload operators, extend classes, use generic types, and all that kind of shit too. I gotta run, maybe i'll post more shit later.

Word.


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: rivon on September 28, 2012, 11:34:22 AM
Thanks guys, it's really helpful. I also discovered this (http://www.codeproject.com/Articles/4300/Quick-C) which taught me a lot.

By the way how is it with the visibilities by default (public, private...)?


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: Wilson Saunders on September 28, 2012, 12:29:04 PM
Variables and Functions default private and must be explicitly made public. Unlike C++ where you can declare a block public.
Code:
public:
int var1;
int var2;
int function(int arg1);
private:
int myVar;
You must put public before each type:
Code:
public int var1;
public int var2;
public int function(int arg1);
private int myVar;

One nifty trick is that all arrays are objects and have a .length value.
Code:
// C# style array decleration
int[] myArray = new int[32];
// use .length parameter to access array length without storing 32
for(int itor = 0; itor < myArray.length; itor++){
   myArray[itor] = itor;
}



Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: BorisTheBrave on September 28, 2012, 01:56:18 PM
There are some important features that are from neither C++ nor Java. Even more if you aren't familiar with the latest Java, which copied several of C#'s featureset. The 5 second brief:

Generics: Similar to Java's but there is no type erasure - they stay as separate types even after compilation

getters/setters: 'nuff said

structs: what C++ users would call a value type, these are copied when passed around

out/ref parameters: clearer syntax that Foo& for an argument.

delegates, lambdas: closer to std::function that function pointers, iirc

LINQ: You probably don't need this right away, but it is SQL-like syntactic sugar, that can be transformed into actual SQL, in-memory methods, etc. In fact, it is the tip of a macro-programming iceberg, afaik.


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: ChevyRay on September 28, 2012, 04:15:12 PM
One nifty trick is that all arrays are objects and have a .length value.

For sure, but it's .Length.

C# Naming Guidelines (http://msdn.microsoft.com/en-us/library/xzf533w0(v=vs.71).aspx)


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: InfiniteStateMachine on September 28, 2012, 06:16:32 PM
I love delegates and lamdas. They are so nice to use.


One thing to note that I've seen some java programmers @ work who switched over is all methods are non-virtual unless marked as virtual. So essentially the opposite of Java.


EDIT: MS also has a quick document noting the major differences between C# and Java

http://msdn.microsoft.com/en-us/library/ms228602(v=vs.90).aspx


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: zalzane on September 28, 2012, 08:03:49 PM
I love delegates and lamdas. They are so nice to use.


One thing to note that I've seen some java programmers @ work who switched over is all methods are non-virtual unless marked as virtual. So essentially the opposite of Java.


EDIT: MS also has a quick document noting the major differences between C# and Java

http://msdn.microsoft.com/en-us/library/ms228602(v=vs.90).aspx

I got your differences right here.

FEATURES C# HAS THAT JAVA DOES NOT
http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx - Value types vs. reference types
http://msdn.microsoft.com/en-us/library/dd264739.aspx - Named and optional arguments
http://msdn.microsoft.com/en-us/library/8627sbea.aspx - Built-in events
http://msdn.microsoft.com/en-us/library/bb397687.aspx - Lambda expressions
http://msdn.microsoft.com/en-us/library/d5x73970.aspx - Value types allowed as generic parameters; generic constraints
http://msdn.microsoft.com/en-us/library/ee207183.aspx - Co/contravariance
http://msdn.microsoft.com/en-us/library/ms173171.aspx - Delegates
http://msdn.microsoft.com/en-us/library/9fkccyh4.aspx - Methods are not overridable by default
http://msdn.microsoft.com/en-us/library/e59b22c5.aspx - Better interop support with unmanaged code
http://msdn.microsoft.com/en-us/library/y31yhkeb.aspx - Pointers
http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx - Properties (getters/setters)
http://msdn.microsoft.com/en-us/library/8edha89s.aspx - Operator overloading
http://msdn.microsoft.com/en-us/library/hh156513.aspx - 'async' methods
http://msdn.microsoft.com/en-us/library/ms229005.aspx - More flexible exception throwing*

http://msdn.microsoft.com/en-us/library/5cyb68cy.aspx - Ability to allocate memory not managed by the GC
http://msdn.microsoft.com/en-us/library/system.io.aspx - Standard IO library that is not complicated and overly verbose

http://www.codethinked.com/c-closures-explained - Closures
http://geekswithblogs.net/sdorman/archive/2007/04/06/111034.aspx - Type Inference
http://msdn.microsoft.com/en-us/library/windows/hardware/hh439574(v=vs.85).aspx - Conditional Metadata
http://msdn.microsoft.com/en-us/library/aa691099).aspx - Conditional Compilation
http://msdn.microsoft.com/en-us/library/wa80x488.aspx - Partial Classes and Methods
http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx - Indexers
http://msdn.microsoft.com/en-us/library/ms173105.aspx - Implicit and Explicit Conversions
http://msdn.microsoft.com/en-us/library/bb384062.aspx - Object and Collection Initalizers
http://msdn.microsoft.com/en-us/library/ms173157.aspx - Explicit interface implementation

FEATURES THAT JAVA HAS THAT C# DOES NOT
Local and anonymous inner classes

================================================================================

* adding 'throws' to a method in Java requires snowballing changes to all methods that make use of that method, which requires changes to those methods, etc...


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: rivon on September 29, 2012, 07:22:19 AM
I said that I'm C++ guy, not Java guy. I only know from it (and use) the C++-like parts aka classes, functions etc. the basic stuff.

But thanks anyway.


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: InfiniteStateMachine on September 29, 2012, 07:36:07 AM
Hey guys, I need to learn C# in a week. I know C++ quite good and I know Java a bit. Do you know any good and quick tutorials for a guy like me?

You mentioned in your original post that you know Java a bit. Since C# and Java have so many similarities that's why the thread went in that direction.

So you don't really know java then? Are you familiar with C++ GC's?

Get ready to give up some control :)


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: rivon on September 29, 2012, 01:27:50 PM
A bit of Java. That means not all. Also, I don't know any C++ GCs. I'm ok with manual memory management.


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: zalzane on September 29, 2012, 02:04:14 PM
automated memory management is probably the best part of moving from a compiled language to managed


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: rivon on September 29, 2012, 02:47:17 PM
Except you compile Java and C# too...


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: zalzane on September 29, 2012, 03:47:54 PM
Except you compile Java and C# too...

They're not literally being compiled in the sense of converting human readable code to machine code. They convert the code to java bytecode and intermediate language, which is then run by the java VM and the JIT interpreter, respectively.


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: Dacke on September 29, 2012, 03:56:03 PM
bytecode is literally machine code. It's just read by a virtual machine rather than a physical machine, but it's still machine code.


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: zalzane on September 29, 2012, 03:59:57 PM
technically, machine code is code that can be run directly by the cpu.


Even with that technicality aside, it would be pretty confusing to refer to c# and java as compiled languages, when in implementation and application, they aren't.


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: Klaim on September 29, 2012, 04:13:33 PM
Isn't the point of JIT that most of the code will be compiled to machine-executable code at some point?


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: Dacke on September 29, 2012, 04:16:07 PM
@zalzane:
Hm, I guess it's a matter of definition (and Wikipedia agrees with yours). Bytecode does fall in its own category.

However, Java/C# have to be compiled by a compiler before you can run them, so I find it less confusing to call them compiled languages.


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: rivon on September 30, 2012, 01:12:05 AM
Except you compile Java and C# too...

They're not literally being compiled in the sense of converting human readable code to machine code. They convert the code to java bytecode and intermediate language, which is then run by the java VM and the JIT interpreter, respectively.
Yeah, I know. It's compiled into bytecode but it still has to be compiled unlike Python or Ruby or something.


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: InfiniteStateMachine on September 30, 2012, 02:37:08 AM
Yeah bytecode languages are kind of their own thing in the middle of interpreted and fully compiled.

Funny story I asked every teacher at my school for their definition of "Managed Languages"

No one had the same answer.


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: ChevyRay on September 30, 2012, 09:29:15 AM
It's worth noting that local variables can be defined using var, like so:

Code:
//ugh boilerplate typing
Dictionary<string, int> lookup = new Dictionary<string, int>();

//equivalent to the above line
var lookup = new Dictionary<string, int>();

These are not dynamic types and are in fact strongly typed, the compiler just decides the type of the variable during compilation. I only ever do this when it's very clear what the type of the variable is (either by the name or like in the code above). I will not usually do it for return types or anything like that where I can't tell at a glance what the type is.

But it's got some pretty cool benefits. Consider the following:

Code:
int[] nums = new int[]{5, 4, 11, 12, 22};

foreach (int n in nums)
{

}

If I later decide that I want to change the value type of "nums" to float, I will have to change both the array definition and the iterator. But if I use var for the iterator instead of typing directly:

Code:
int[] nums = new int[]{5, 4, 11, 12, 22};

foreach (var n in nums)
{

}

Now I can easily change the array type to float...

Code:
float[] nums = new float[]{5, 4.5f, 11, 12.6f, 22};

foreach (var n in nums)
{

}

And now the compiler will just change what type "n" is compiling as from int to float.


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: InfiniteStateMachine on September 30, 2012, 09:51:51 PM
that's pretty cool! Never thought to to do that.


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: EdgeOfProphecy on October 03, 2012, 11:30:51 AM
automated memory management is probably the best part of moving from a compiled language to managed

Ehhhhh sort of.  I don't think it really changes that much since you're still going to have to control the lifecycle of your objects in C#.  Memory can still leak, and you can still clobber available memory if you just allocate objects all over the place.


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: Dacke on October 03, 2012, 11:43:06 AM
As a side note: Memory management and machine-code compilation aren't mutually exclusive. The language Go (http://en.wikipedia.org/wiki/Go_(programming_language)) has both, and as such it is the language I'm currently trying to learn.


Title: Re: Quick and easy C# tutorial for a C++ guy?
Post by: InfiniteStateMachine on October 04, 2012, 05:58:04 AM
c++ has plenty of custom memory managers and garbage collectors too. They just aren't built directly into the language.

Was reading somewhere that a optional GC was proposed as part of the new C++ spec but they couldnt agree on its implementation in time.