我想将Timespan转换为Datetime.我怎样才能做到这一点?
我在Google上找到了一种方法:
DateTime dt;
TimeSpan ts="XXX";
//We can covnert 'ts' to 'dt' like this:
dt= Convert.ToDateTime(ts.ToString());
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以做到这一点?
在序列化期间,我们可以使用内存流或文件流.
这两者之间的基本区别是什么?记忆流意味着什么?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
namespace Serilization
{
class Program
{
static void Main(string[] args)
{
MemoryStream aStream = new MemoryStream();
BinaryFormatter aBinaryFormat = new BinaryFormatter();
aBinaryFormat.Serialize(aStream, person);
aStream.Close();
}
}
}
Run Code Online (Sandbox Code Playgroud) 这可能是基本问题
要在多线程环境中使用单例,我们可以使用锁.请参阅代码段.但为什么我们需要在单件模式中进行双重检查锁定?更多的双重锁定意味着什么?
class singleton
{
private static singleton instance = null;
private static singleton() { }
private static object objectlock = new object();
public static singleton Instance
{
get
{
lock (objectlock) //single - check lock
{
if (instance == null)
{
instance = new singleton();
}
return instance;
}
}
}
}
Run Code Online (Sandbox Code Playgroud) using System;
using System.Reflection;
namespace Reflection
{
class Test
{
protected void methodname(int i)
{
Console.WriteLine(("in the world of the reflection- only i"));
Console.Read();
}
protected void methodname(int i, int j)
{
Console.WriteLine(("in the world of the reflection i , j"));
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
// BindingFlags eFlags = BindingFlags.Default | BindingFlags.Instance | BindingFlags.Public|BindingFlags.NonPublic;
BindingFlags eFlags = BindingFlags.Instance|BindingFlags.NonPublic;
Test aTest = new Test();
MethodInfo mInfoMethod = typeof(Reflection.Test).GetMethod("methodname", eFlags);
mInfoMethod.Invoke(aTest, new object[] { 10 …
Run Code Online (Sandbox Code Playgroud) class Point
{
private int m_PointX;
private int m_PointY;
public Point(int x, int y)
{
m_PointX = x;
m_PointY = y;
}
public static Point operator+(Point point1, Point point2)
{
Point P = new Point();
P.X = point1.X + point2.X;
P.Y = point1.Y + point2.Y;
return P;
}
}
Run Code Online (Sandbox Code Playgroud)
例:
Point P1 = new Point(10,20);
Point P2 = new Point(30,40)
P1+P2; // operator overloading
Run Code Online (Sandbox Code Playgroud)
在德语langauage-十进制分隔符,值分隔符是";" .在英语/其他语言中,十进制分隔符是.和值分隔符是",".
我想创建一个独立于当前文化的.csv文件.我的意思是.csv文件总是应该有"." 有小数分隔符和","有值分隔符.
下面给出的代码.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
namespace CSV_FILE_FORMAT
{
class Program
{
static void Main(string[] args)
{
string aFileName = "result.csv";
FileStream aFileStream = new FileStream(aFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
StreamWriter m_StreamWriter = new StreamWriter(aFileStream);
double[] values = new double[] { 10.5, 20.3, 30.2 };
for(int i = 0; i < values.Length;i++)
{
m_StreamWriter.Write(values[i].ToString(CultureInfo.InvariantCulture));
m_StreamWriter.Write(",");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码的问题在于操作系统是德语.小数分隔符显示为","而不是".".
请让我知道代码遗漏了什么.
using System;
using System.Reflection;
namespace Reflection
{
class Test
{
protected void methodname()
{
Console.WriteLine(("in the world of the reflection"));
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
// BindingFlags eFlags = BindingFlags.Default | BindingFlags.Instance | BindingFlags.Public|BindingFlags.NonPublic;
BindingFlags eFlags = BindingFlags.Instance|BindingFlags.NonPublic;
Test aTest = new Test();
MethodInfo mInfoMethod = typeof(Reflection.Test).GetMethod("methodname", eFlags);
mInfoMethod.Invoke(aTest, null);
}
}
}
Run Code Online (Sandbox Code Playgroud)
根据msdn BindingFlags.Nonpublic用于访问非私有成员。如果仅使用此枚举,则Getmethod返回空值。但是,如果使用枚举-实例和非公共,则将调用所需的方法。两者之间有什么区别。当我必须使用实例和公共/非公共组合时。
根据 MSDN ,以下字符不能作为文件名的一部分:
\n\n\n\n\n使用当前代码页中的任何字符作为名称,包括 Unicode 字符和扩展字符集中的字符 (128\xe2\x80\x93255),但以下字符除外:
\n\n\xe2\x97\xa6以下保留字符:
\n\n\n
\n- \n
<
(少于)- \n
>
(比...更棒)- \n
:
(冒号)- \n
"
(双引号)- \n
/
(正斜杠)- \n
\\
(反斜杠)- \n
|
(竖条或竖管)- \n
?
(问号)- \n
*
(星号)
在 .net 中,提供了一个 api 来查找文件名中不允许使用的字符
\n\nchar[] invalidFileChars = Path.GetInvalidFileNameChars();\n
Run Code Online (Sandbox Code Playgroud)\n\n\n\n\n评论
\n\n不保证从此方法返回的数组包含文件名和目录名中无效的完整字符集。完整的无效字符集可能因文件系统而异。例如,在基于 Windows 的桌面平台上,无效路径字符可能包括 ASCII/Unicode 字符 1 到 31,以及引号 (")、小于 (<)、大于 (>)、竖线 (|)、退格 ( \\b)、空 (\\0) 和制表符 (\\t)。
\n
但在备注部分说它取决于文件系统。
\n\n在 XP 和 …
public class Base1
{
public virtual void f()
{
Console.WriteLine("Base1.f()");
}
}
public class Derived1 : Base1
{
// Hides Base1.f() because 'override' was not specified
public new virtual void f()
{
Console.WriteLine("Derived1.f()");
}
}
public class Derived2 : Derived1
{
// Overrides Derived1.f()
public override void f()
{
Console.WriteLine("Derived2.f()");
// Call base method
base.f();
}
}
class Program
{
static void Main(string[] args)
Base1 ob1 = new Derived1();
ob1.f();
Base1 ob2 = new Derived2();
ob2.f();
Derived1 ob3 = new …
Run Code Online (Sandbox Code Playgroud) string s = "value_test_this";
string m = s.Replace('e','E');
StringBuilder strBuilder = new StringBuilder("value_test_this");
strBuilder.Replace('e','E');
Run Code Online (Sandbox Code Playgroud)
因为字符串是不可变的,所以Replace
在字符串类中如何工作,
c# ×10
reflection ×2
datetime ×1
filenames ×1
filestream ×1
inheritance ×1
memorystream ×1
timespan ×1