0

So ive got a few questions about CLI In visual studio it called CLR..

My First Question, what is the XOR Sign? (^), I know that it a bitwise operator But when im doing let's say

int ^ A = 5;
int B = 5;

Console::WriteLine(A->ToString());
Console::WriteLine(B.ToString());

What are the diffrences beetwen those 2? It's like pointers in C?

Question 2:

MyClass ^ MC1 = gcnew MyClass(); //What is the advantage of using gcnew?
MyClass MC2 = new MyClass(); //Why 'new' keyword dosent work on CLI? or maybe there is another way of using it?

Question 3:

Int32 B(int i)
{
    return i;
}
Int32 ^ C(int ^ i)
{
    return i;
}

What is the diffrence beetwen returnning ^ Variable and Normal Variable?

Question 4:

void StringFunc1(String ^ S)
{
    Console::WriteLine(S);
}
void StringFunc2(String S)
{
    Console::WriteLine(S); //Why a normal string dosent work?
}
Console::WriteLine("Hello World");
Console::WriteLine(L"Hello World");

What are the diffrences beetwen L"Hello World" and "Hello World"? What's the 'L' For? Why a normal string dosent work on 'StringFunc2'?

Question 5:

MyClass ^ MC1 = gcnew MyClass();
MyClass ^ MC2 = gcnew MyClass();
int i[] = { 1, 2, 3 };
int ii[,] = { { 1, 2, 3 }, { 4, 5, 6 } }; //How do i write dimensional array?
String s[] = { "Hey", "Dude" }; //Why string array dosent work?
MyClass MCArray[] = { MC1, MC2 }; //Why MyClass array dont work?
crashmstr
  • 28,043
  • 9
  • 61
  • 79
Pinkie Pie
  • 61
  • 4

2 Answers2

1

My First Question, what is the XOR Sign? (^), I know that it a bitwise operator But when im doing let's say

int ^ A = 5;
int B = 5;

Console::WriteLine(A->ToString());
Console::WriteLine(B.ToString());

What are the diffrences beetwen those 2? It's like pointers in C?

The ^ is used for managed references, which are like pointers but refer to objects stored on the managed heap. For value types line int, you don't need to use a reference, but if you do, the object is boxed.

Question 2:

MyClass ^ MC1 = gcnew MyClass(); //What is the advantage of using gcnew?
MyClass MC2 = new MyClass(); //Why 'new' keyword dosent work on CLI? or maybe there is another way of using it?

gcnew is the equivalent of "new" for managed types. gcnew only works for managed types and "new" only works for unmanaged types.

Question 3:

Int32 B(int i)
{
    return i;
}
Int32 ^ C(int ^ i)
{
    return i;
}

What is the diffrence beetwen returnning ^ Variable and Normal Variable?

Same as in question #1. The first function returns the object in value type form, the second one returns a boxed Int32.

Question 4:

void StringFunc1(String ^ S)
{
    Console::WriteLine(S);
}
void StringFunc2(String S)
{
    Console::WriteLine(S); //Why a normal string dosent work?
}
Console::WriteLine("Hello World");
Console::WriteLine(L"Hello World");

What are the diffrences beetwen L"Hello World" and "Hello World"? What's the 'L' For? Why a normal string dosent work on 'StringFunc2'?

Normally the L prefix is used for wide-character (wchar_t) strings, but for managed string literals you don't need it. The compiler will recognize that it is being used where a System::String is expected, and will just create a System::String instead of a raw character array.

Also you need the hat (^) because System::String is a reference type, and you can't unbox reference types.

Question 5:

MyClass ^ MC1 = gcnew MyClass();
MyClass ^ MC2 = gcnew MyClass();
int i[] = { 1, 2, 3 };
int ii[,] = { { 1, 2, 3 }, { 4, 5, 6 } }; //How do i write dimensional array?
String s[] = { "Hey", "Dude" }; //Why string array dosent work?
MyClass MCArray[] = { MC1, MC2 }; //Why MyClass array dont work?

The [] syntax doesn't work for managed types. You need to use the array keyword:

array<int>^ i = { 1, 2, 3 };

To create a multi-dimentional array:

array<int, 2>^ ii = { { 1, 2, 3 }, { 4, 5, 6 } };

To create an array of strings:

array<String^>^ s = { "a", "b" };

To create an array reference types:

array<MyClass^>^ mc = { gcnew MyClass(), gcnew MyClass() };
Community
  • 1
  • 1
user1610015
  • 6,561
  • 2
  • 15
  • 18
0

The operator ^ is the managed equivalent of a pointer in C++. It's called a Handle to a reference type.The gcnew allocate an instance of a type which will be garbage collected when it will become inaccessible. The L"Hello World" means that hello world will be a whar_t literal If you want a array you can declare it like that
array<int>^ myArray= gcnew array<int>(x); for(int i=0; i<x; i++) myArray[i] = i;

If you want a two dimensional array you go like that
array<String^,2>^ myStringBiDimensionalArray = gcnew array<String^,2>(4,2); myStringBiDimensionalArray [0,0] = "Something"; myStringBiDimensionalArray [1,0] = "Something else"; myStringBiDimensionalArray [2,0] = "42"; myStringBiDimensionalArray [3,0] = "Why not zoidberg";

Pumkko
  • 1,523
  • 15
  • 19
  • Thanks so much, i appriciate your help. By the way, does CLI Is really a .NET? Or it's a C++ Native like Win32? – Pinkie Pie Jul 20 '15 at 12:01
  • I think this post will help you more than I will, http://stackoverflow.com/questions/5693477/ccli-are-native-parts-written-in-pure-c-but-compiled-in-cli-as-fast-as-pure. – Pumkko Jul 20 '15 at 13:25