Interesting 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
{
}
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 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 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);
}
Object x = Activator.CreateInstance(typeof(string));
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);
}
}
}
[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
public int SetValue(UInt32 value);
If the declaration is marked with a CLSCompliantAttribute, no compiler warning or error is generated.
{
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);
}
Present compilers .......
{
return (i.ToString() );
}
main()
{
int[] apf = { 23,43,434};
string[] ap = Array.ConvertAll(apf, new Converter<int, string>(Myconverter));
}
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;
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.
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.