Interesting C#

You can write this C++ code in C#
static unsafe void Main(string[] args)
{
int* myarray = stackalloc int[100];
int* crawler = myarray;
*crawler++ = *crawler++ = 1;
for (int i = 2; i < 100; ++i, ++crawler)
*crawler = crawler[-1] + crawler[-2];
}
btw.. your project settings need to allow unsafe. This method level unsafe usage.


You can also do -- class level unsafe code.
unsafe class Program
{
}
object p = new Alpha("test");
Alpha q = (Alpha)p;
Alpha r = p as Alpha ;
if (p is Alpha)
{
//No need of try catch
Alpha s = (Alpha)p;
}
try
{
// need for try catch
Alpha t = (Alpha)p;
}
catch (Exception err)
{
}
public delegate void Showit();

public class simpleclass
{
private string a;
public simpleclass(string name)
{
a = name;
}
public void messager()
{
MessageBox.Show(a);
}
public void silencer()
{
Console.WriteLine(a);
}
}

main()
{
simpleclass z = new simpleclass("John");
Showit try1 = z.messager;
Showit try2 = z.silencer;
try1();
try2();
}
public class SomeType
{
public void DoSomething(int x)
{
MessageBox.Show("Good");
}
}
main()
{
Object x = Activator.CreateInstance(typeof(StringBuilder));
StringBuilder b = (StringBuilder)x; b.Append("hi");
string y = b.ToString() ;
MessageBox.Show(y);

System.Runtime.Remoting.ObjectHandle oh =
Activator.CreateInstanceFrom(
Assembly.GetEntryAssembly().CodeBase,
typeof(SomeType).FullName);

SomeType st = (SomeType) oh.Unwrap();

st.DoSomething(5);
}
Do not try following in the above example
Object x = Activator.CreateInstance(typeof(string));
//Use of attributes for runtime info
public enum Actions
{
swim = 1,
walk,
eat,
}
public class ActivityTypeAttribute : Attribute
{
public ActivityTypeAttribute(Actions pwork)
{
work = pwork;
}
protected Actions work;
public Actions Work
{
get { return work; }
set { work = Work; }
}
}

class Olympics
{
[ActivityType(Actions.swim)]
public void swimmer() { }

[ActivityType(Actions.walk)]
public void walker() { }

[ActivityType(Actions.eat)]
public void eater() { }
}

main()
{
Olympics testClass = new Olympics();
Type type = testClass.GetType();
foreach (MethodInfo function in type.GetMethods())
{
foreach (Attribute attr in Attribute.GetCustomAttributes(function))
{
if (attr.GetType() == typeof(ActivityTypeAttribute))
MessageBox.Show( function.Name+((ActivityTypeAttribute)attr).Work);
}
}
}
The Attribute class associates predefined system information or user-defined custom information with a target element. A target element can be an assembly, class, constructor, delegate, enum, event, field, interface, method, portable executable file module, parameter, property, return value, struct, or another attribute.
// This attribute is only valid on a class.
[AttributeUsage(AttributeTargets.Class)]
public class ClassTargetAttribute:Attribute
{
}

AttributeTargets.Class
AttributeTargets.Struct
AttributeTargets.Enum
AttributeTargets.Constructor
AttributeTargets.Method
AttributeTargets.property.
AttributeTargets.Field
AttributeTargets.Event
AttributeTargets.Interface
AttributeTargets.Parameter
AttributeTargets.Delegate
AttributeTargets.ReturnValue
AttributeTargets.GenericParameter
AttributeTargets.All
[CLSCompliant(false)]
public int SetValue(UInt32 value);
If the declaration is marked with a CLSCompliantAttribute, no compiler warning or error is generated.
private static int CompareByname(string x, string y)
{
return x.CompareTo(y);
}
private static int CompareByID(int x, int y)
{
return x.CompareTo(y);
}

main()
{
List<string> dogs = new List<string>();
dogs.Add("good");
dogs.Add("bad");
dogs.Add("");
dogs.Add("red");
dogs.Add("pink");
dogs.Add("humble");
dogs.Sort(CompareByname);

List<Int32 > cats = new List<Int32 >();
cats.Add(55);
cats.Add(64);
cats.Add(43);
cats.Add(76);
cats.Add(644);
cats.Add(655);
cats.Sort(CompareByID);
}
The current version of the common language runtime does not support generic ContextBoundObject types or nongeneric ContextBoundObject types that have generic methods. Attempting to create an instance of such a type causes a TypeLoadException.


Present compilers .......
public static string Myconverter(int i)
{
return (i.ToString() );
}

main()
{
int[] apf = { 23,43,434};
string[] ap = Array.ConvertAll(apf, new Converter<int, string>(Myconverter));
}
enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };
enum BoilingPoints { Celcius = 100, Fahrenheit = 212 };
[FlagsAttribute]
enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };
Use the FlagsAttribute custom attribute for an enumeration only if a bitwise operation (AND, OR, EXCLUSIVE OR) is to be performed on a numeric value.

Colors myColors = Colors.Red | Colors.Blue | Colors.Yellow;
ENVIRONMENT CLASS

HasShutdownStarted Gets a value indicating whether the common language runtime is shutting down or the current application domain is unloading.
MachineName Gets the NetBIOS name of this local computer.
NewLine Gets the newline string defined for this environment.
OSVersion Gets an OperatingSystem object that contains the current platform identifier and version number.
ProcessorCount Gets the number of processors on the current machine.
StackTrace Gets current stack trace information.
SystemDirectory Gets the fully qualified path of the system directory.
TickCount Gets the number of milliseconds elapsed since the system started.
UserDomainName Gets the network domain name associated with the current user.
UserInteractive Gets a value indicating whether the current process is running in user interactive mode.
UserName Gets the user name of the person who is currently logged on to the Windows operating system.
Version Gets a Version object that describes the major, minor, build, and revision numbers of the common language runtime.
WorkingSet Gets the amount of physical memory mapped to the process context.
COM threading models only pertain to applications that use COM interop. Using this attribute in an application that does not use COM interop has no effect.

The COM threading model can be set to single-threaded apartment or multithreaded apartment. The application thread is only initialized for COM interop if the thread actually makes a call to a COM component. If COM interop is not used, then the thread is not initialized.