View Full Version : آموزش: مطالبی در مورد C# که احتمالا از آنها آگاه نبوده اید!
mehdi.mousavi
شنبه 08 اسفند 1388, 12:21 عصر
سلام.
این عنوان پستی هستش که اینجا (http://www.devtheweb.net/blog/2010/02/25/things-you-probably-didnt-know-about-csharp/)آورده شده. من فقط از شماره یک بی خبر بودم. شماره یک رو اینجا میذارم، چون کار خود منو خیلی ساده تر میکنه:
1. جای اینکه بنویسیم:
using (Font f1 = new Font("Arial", 10.0f))
{
using (Font f2 = new Font("Calibri", 10.0f))
{
//use f1 and f2 here
}
}
میتونیم بنویسیم:
using (Font f1 = new Font("Arial", 10.0f), f2 = new Font("Calibri", 10.0f))
{
//use f1 and f2 here
}
دقت کنید، که نباید Data Type متغیر دوم رو Explicitly ذکر کنیم و باید هر دو متغیر از یک Data Type باشن (که در پست مزبور به این مساله اشاره نکرده و کد نوشته شده در شماره یک این پست، این ایراد رو داره).
موفق باشید.
r00tkit
شنبه 08 اسفند 1388, 16:12 عصر
به نظر شما کدم تابع اجرا می شه
namespace ConsoleApplication2
{
class cl<T>
{
public static void m1(T x)
{
Console.WriteLine("m1 "+x);
}
public static void m1(int x)
{
Console.WriteLine("m2 "+x);
}
}
class Program
{
static void Main(string[] args)
{
cl<int>.m1(10);
}
}
}
r00tkit
شنبه 08 اسفند 1388, 16:13 عصر
چند روز پیش توابع net. رو با reflector بررسی می کردم دیدم
منع اکثر توابع net. برمی گرده به :
توی assembly mscorlib.dll
=> namespace Microsoft.Win32
یه کلاس internalهست که توابع ان کلاس اکثرا pinvke هستنند
مثلا Console.Beep(); بر میگرده به متود Win32Native.beep
اینم پیاده سازی این متود
[DllImport("kernel32.dll", SetLastError=true)] internal static extern bool Beep(int frequency, int duration);
mehdi.mousavi
شنبه 08 اسفند 1388, 16:44 عصر
به نظر شما کدم تابع اجرا می شه
سلام.
اینجا تابع دوم اجرا میشه. یعنی اونی که Explicitly داره میگه پارامترش int هست.
پاورقی: اینو خیلی وقت پیش خونده بودم :)
r00tkit
شنبه 08 اسفند 1388, 19:31 عصر
Hidden Features of C# (http://stackoverflow.com/questions/9033/hidden-features-of-c)
r00tkit
یک شنبه 09 اسفند 1388, 16:45 عصر
ایجاد یه ارایه به صورت Non-Zero-Lower Bound
static void Main(string[] args)
{
int[] myLengthsArray = new int[1] { 5 };
int[] myBoundsArray = new int[1] { 1 };
Array myArray = Array.CreateInstance(typeof(String), myLengthsArray, myBoundsArray);
for (int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++)
{
myArray.SetValue(i.ToString(), i);
}
Console.WriteLine(myArray.Length);
for (int i = 1; i < 6; i++)
Console.WriteLine(myArray.GetValue(i));
}
r00tkit
پنج شنبه 13 اسفند 1388, 15:46 عصر
کامپایل متن در داخل برنامه یه snippet compiler
CSharpCodeProvider codeProvider = new CSharpCodeProvider(); ICodeCompiler icc = codeProvider.CreateCompiler();
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
CompilerResults results = icc.CompileAssemblyFromSource(parameters, SourceString);
r00tkit
جمعه 14 اسفند 1388, 21:26 عصر
سلام به همه
dynamically load a native dll from a user specified directory
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllname);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
public static extern IntPtr GetProcAddress(IntPtr loadeddll, string funnam);
delegate void dl(int x, int y);
static void Main()
{
IntPtr x=LoadLibrary("kernel32.dll");
IntPtr y=GetProcAddress(x,"Beep");
Delegate function = Marshal.GetDelegateForFunctionPointer(y, typeof(dl));
dl f= (dl)function;
f(44, 100);
}
vBulletin® v4.2.5, Copyright ©2000-1403, Jelsoft Enterprises Ltd.