小编Rag*_*v55的帖子

TimeSpan到DateTime的转换

我想将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)

有没有其他方法可以做到这一点?

c# datetime timespan

58
推荐指数
5
解决办法
15万
查看次数

内存流和文件流之间的差异

在序列化期间,我们可以使用内存流或文件流.

这两者之间的基本区别是什么?记忆流意味着什么?

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)

c# memorystream filestream

52
推荐指数
6
解决办法
8万
查看次数

单一模式中的双重检查锁定

这可能是基本问题

要在多线程环境中使用单例,我们可以使用锁.请参阅代码段.但为什么我们需要在单件模式中进行双重检查锁定?更多的双重锁定意味着什么?

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)

c#

20
推荐指数
3
解决办法
2万
查看次数

如何使用反射获得重载的私有/受保护方法

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)

c# reflection

12
推荐指数
2
解决办法
1万
查看次数

运算符在C#中重载

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)
  1. 是否有必要始终将运算符重载函数声明为静态?这背后的原因是什么?
  2. 如果我想重载+接受像2 + P2这样的表达式,怎么做?

c# operator-overloading

10
推荐指数
1
解决办法
2494
查看次数

编写.csv文件 - 独立于文化

在德语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)

此代码的问题在于操作系统是德语.小数分隔符显示为","而不是".".

请让我知道代码遗漏了什么.

c#

6
推荐指数
2
解决办法
5951
查看次数

反映受保护类的成员

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返回空值。但是,如果使用枚举-实例和非公共,则将调用所需的方法。两者之间有什么区别。当我必须使用实例和公共/非公共组合时。

c# reflection

5
推荐指数
1
解决办法
4035
查看次数

Windows 上的文件名中存在无效字符?

根据 MSDN ,以下字符不能作为文件名的一部分:

\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
\n\n

在 .net 中,提供了一个 api 来查找文件名中不允许使用的字符

\n\n
char[] invalidFileChars = Path.GetInvalidFileNameChars();\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

评论

\n\n

不保证从此方法返回的数组包含文件名和目录名中无效的完整字符集。完整的无效字符集可能因文件系统而异。例如,在基于 Windows 的桌面平台上,无效路径字符可能包括 ASCII/Unicode 字符 1 到 31,以及引号 (")、小于 (<)、大于 (>)、竖线 (|)、退格 ( \\b)、空 (\\0) 和制表符 (\\t)。

\n
\n\n

但在备注部分说它取决于文件系统。

\n\n

在 XP 和 …

c# filenames special-characters

5
推荐指数
1
解决办法
2万
查看次数

C#中的虚函数

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)

c# inheritance virtual-functions

4
推荐指数
1
解决办法
242
查看次数

string类替换和Stringbuilder替换

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#

3
推荐指数
1
解决办法
366
查看次数